C++ XML文件解析操作
2015-12-24 17:00
489 查看
1、XML文件格式:
<?xml version="1.0" ?>
<root>
<Panel PanelName="WoodPanel" Length="2440" Width="1220" Thick="15" />
<Panel PanelName="AluPanel" Length="2440" Width="1220" Thick="15" Height="20" />
</root>
2、XML文件生成操作
void WriteXml()
{
/*第一种
TiXmlDocument doc;
//根节点
TiXmlElement* root = new TiXmlElement("Panel");
doc.LinkEndChild(root);
//第一节点
TiXmlElement* WoodPanelElement = new TiXmlElement("WoodPanel");
root->LinkEndChild(WoodPanelElement);
//第二节点
TiXmlElement* LengthElement = new TiXmlElement("Length");
WoodPanelElement->LinkEndChild(LengthElement);
TiXmlElement* WidthElement = new TiXmlElement("Width");
WoodPanelElement->LinkEndChild(WidthElement);
TiXmlElement* ThickElement = new TiXmlElement("Thick");
WoodPanelElement->LinkEndChild(ThickElement);
doc.SaveFile("C:\\Users\\user\\Desktop\\重写\\b.xml");
doc.Clear();
*/
/*第二种
const char* xmlFile = "C:\\Users\\user\\Desktop\\重写\\b.xml";
TiXmlDocument doc;
TiXmlDeclaration* decl = new TiXmlDeclaration("1.0", "", "");
doc.LinkEndChild(decl);
TiXmlElement* firstLevel = new TiXmlElement("root");
for (int i = 0; i < 1; ++i)
{
TiXmlElement* secondLevel = new TiXmlElement("Panel");
secondLevel->SetAttribute("PanelName", "WoodPanel");
for (int j = 0; j < 3; ++j)
{
TiXmlElement* thirdLevel = new TiXmlElement(AttributeName[j]);
thirdLevel->LinkEndChild(new TiXmlText(Attribute[i][j]));
secondLevel->LinkEndChild(thirdLevel);
}
firstLevel->LinkEndChild(secondLevel);
}
doc.LinkEndChild(firstLevel);
doc.SaveFile(xmlFile);
*/
//第三种
const char* xmlFile = "C:\\Users\\user\\Desktop\\重写\\b.xml";
TiXmlDocument doc;
TiXmlDeclaration* decl = new TiXmlDeclaration("1.0", "", "");
doc.LinkEndChild(decl);
TiXmlElement* firstLevel = new TiXmlElement("root");
for (int i = 0; i < 1; ++i)
{
TiXmlElement* secondLevel = new TiXmlElement("Panel");
secondLevel->SetAttribute("PanelName", "WoodPanel");
secondLevel->SetAttribute("Length", "2440");
secondLevel->SetAttribute("Width", "1220");
secondLevel->SetAttribute("Thick", "15");
firstLevel->LinkEndChild(secondLevel);
}
doc.LinkEndChild(firstLevel);
doc.SaveFile(xmlFile);
}
3、XML文件解析操作
void ParseXML1(map<string, vector<double>>& map1)
{
const char * xmlFile = "C:\\Users\\user\\Desktop\\重写\\b.xml";
TiXmlDocument doc;
doc.LoadFile(xmlFile);
TiXmlElement* firstLevel = doc.RootElement();
TiXmlElement* secondLevel = firstLevel->FirstChildElement();
while (secondLevel != NULL)
{
TiXmlAttribute* pAttribute = secondLevel->FirstAttribute(); // 获得属性及数值
//std::cout << pAttribute->Name() << endl;
const char* ccAttribute = pAttribute->Value();
string str(ccAttribute);
std::cout << pAttribute->Value() << endl;
vector<double> vec1;
TiXmlAttribute* pAttributeSecond = pAttribute->Next(); // 获得属性及数值
while (pAttributeSecond)
{
std::cout << pAttributeSecond->Value() << endl;
const char* ccAttributeSecond = pAttributeSecond->Value();
double dAttributeSecond = atof(ccAttributeSecond);
vec1.push_back(dAttributeSecond);
pAttributeSecond = pAttributeSecond->Next();
}
map1[str] = vec1;
//delete pAttributeSecond;
secondLevel = secondLevel->NextSiblingElement();
}
//delete secondLevel;
//delete firstLevel;
}
4、数组元素应用操作
map<string, vector<double>> ConfigInfoMap;
ParseXML1(ConfigInfoMap);
/*
for (map<string, vector<double>>::iterator itrMap = ConfigInfoMap.begin(); itrMap != ConfigInfoMap.end(); itrMap++)
{
if (0==(itrMap->first).compare("WoodPanel"))
{
cout << itrMap->first;
for (vector<double>::iterator itrVec = itrMap->second.begin(); itrVec != itrMap->second.end(); itrVec++)
{
std::cout << *(itrVec); //读取数组元素操作
std::cout << ";";
std::cout << endl;
}
}
}*/
for (map<string, vector<double>>::iterator itrMap = ConfigInfoMap.begin(); itrMap != ConfigInfoMap.end(); itrMap++)
{
if (0 == (itrMap->first).compare("WoodPanel"))
{
cout << (itrMap->second)[0]<<endl;
cout << (itrMap->second)[1] << endl;
cout << (itrMap->second)[2] << endl;
}
}
<?xml version="1.0" ?>
<root>
<Panel PanelName="WoodPanel" Length="2440" Width="1220" Thick="15" />
<Panel PanelName="AluPanel" Length="2440" Width="1220" Thick="15" Height="20" />
</root>
2、XML文件生成操作
void WriteXml()
{
/*第一种
TiXmlDocument doc;
//根节点
TiXmlElement* root = new TiXmlElement("Panel");
doc.LinkEndChild(root);
//第一节点
TiXmlElement* WoodPanelElement = new TiXmlElement("WoodPanel");
root->LinkEndChild(WoodPanelElement);
//第二节点
TiXmlElement* LengthElement = new TiXmlElement("Length");
WoodPanelElement->LinkEndChild(LengthElement);
TiXmlElement* WidthElement = new TiXmlElement("Width");
WoodPanelElement->LinkEndChild(WidthElement);
TiXmlElement* ThickElement = new TiXmlElement("Thick");
WoodPanelElement->LinkEndChild(ThickElement);
doc.SaveFile("C:\\Users\\user\\Desktop\\重写\\b.xml");
doc.Clear();
*/
/*第二种
const char* xmlFile = "C:\\Users\\user\\Desktop\\重写\\b.xml";
TiXmlDocument doc;
TiXmlDeclaration* decl = new TiXmlDeclaration("1.0", "", "");
doc.LinkEndChild(decl);
TiXmlElement* firstLevel = new TiXmlElement("root");
for (int i = 0; i < 1; ++i)
{
TiXmlElement* secondLevel = new TiXmlElement("Panel");
secondLevel->SetAttribute("PanelName", "WoodPanel");
for (int j = 0; j < 3; ++j)
{
TiXmlElement* thirdLevel = new TiXmlElement(AttributeName[j]);
thirdLevel->LinkEndChild(new TiXmlText(Attribute[i][j]));
secondLevel->LinkEndChild(thirdLevel);
}
firstLevel->LinkEndChild(secondLevel);
}
doc.LinkEndChild(firstLevel);
doc.SaveFile(xmlFile);
*/
//第三种
const char* xmlFile = "C:\\Users\\user\\Desktop\\重写\\b.xml";
TiXmlDocument doc;
TiXmlDeclaration* decl = new TiXmlDeclaration("1.0", "", "");
doc.LinkEndChild(decl);
TiXmlElement* firstLevel = new TiXmlElement("root");
for (int i = 0; i < 1; ++i)
{
TiXmlElement* secondLevel = new TiXmlElement("Panel");
secondLevel->SetAttribute("PanelName", "WoodPanel");
secondLevel->SetAttribute("Length", "2440");
secondLevel->SetAttribute("Width", "1220");
secondLevel->SetAttribute("Thick", "15");
firstLevel->LinkEndChild(secondLevel);
}
doc.LinkEndChild(firstLevel);
doc.SaveFile(xmlFile);
}
3、XML文件解析操作
void ParseXML1(map<string, vector<double>>& map1)
{
const char * xmlFile = "C:\\Users\\user\\Desktop\\重写\\b.xml";
TiXmlDocument doc;
doc.LoadFile(xmlFile);
TiXmlElement* firstLevel = doc.RootElement();
TiXmlElement* secondLevel = firstLevel->FirstChildElement();
while (secondLevel != NULL)
{
TiXmlAttribute* pAttribute = secondLevel->FirstAttribute(); // 获得属性及数值
//std::cout << pAttribute->Name() << endl;
const char* ccAttribute = pAttribute->Value();
string str(ccAttribute);
std::cout << pAttribute->Value() << endl;
vector<double> vec1;
TiXmlAttribute* pAttributeSecond = pAttribute->Next(); // 获得属性及数值
while (pAttributeSecond)
{
std::cout << pAttributeSecond->Value() << endl;
const char* ccAttributeSecond = pAttributeSecond->Value();
double dAttributeSecond = atof(ccAttributeSecond);
vec1.push_back(dAttributeSecond);
pAttributeSecond = pAttributeSecond->Next();
}
map1[str] = vec1;
//delete pAttributeSecond;
secondLevel = secondLevel->NextSiblingElement();
}
//delete secondLevel;
//delete firstLevel;
}
4、数组元素应用操作
map<string, vector<double>> ConfigInfoMap;
ParseXML1(ConfigInfoMap);
/*
for (map<string, vector<double>>::iterator itrMap = ConfigInfoMap.begin(); itrMap != ConfigInfoMap.end(); itrMap++)
{
if (0==(itrMap->first).compare("WoodPanel"))
{
cout << itrMap->first;
for (vector<double>::iterator itrVec = itrMap->second.begin(); itrVec != itrMap->second.end(); itrVec++)
{
std::cout << *(itrVec); //读取数组元素操作
std::cout << ";";
std::cout << endl;
}
}
}*/
for (map<string, vector<double>>::iterator itrMap = ConfigInfoMap.begin(); itrMap != ConfigInfoMap.end(); itrMap++)
{
if (0 == (itrMap->first).compare("WoodPanel"))
{
cout << (itrMap->second)[0]<<endl;
cout << (itrMap->second)[1] << endl;
cout << (itrMap->second)[2] << endl;
}
}
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- XML 与 JSON 优劣对比
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- As3.0 xml + Loader应用代码
- DVI 视频接口图文解析
- IE6不能正常解析CSS文件问题的解决方法及原因分析
- 网马生成器 MS Internet Explorer XML Parsing Buffer Overflow Exploit (vista) 0day
- ext读取两种结构的xml的代码
- C#自写的一个HTML解析类(类似XElement语法)
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- C#针对xml基本操作及保存配置文件应用实例
- ruby 过程对象 解析
- Ruby程序中创建和解析XML文件的方法
- Lua教程(二):C++和Lua相互传递数据示例
- asp下查询xml的实现代码
- sqlserver FOR XML PATH 语句的应用