C++ XML文件解析操作
来源:互联网 发布:现在淘宝卖什么赚钱 编辑:程序博客网 时间:2024/06/05 14:32
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;
}
}
- dom解析XML文件操作
- C++ XML文件解析操作
- jdom去操作xml文件 解析XML
- C语言解析XML文件
- XML文件解析C++Binding
- C语言解析.XML文件
- c语言解析xml文件
- C#XML文件读写操作
- C#XML文件读写操作
- C语言文件操作解析
- C语言文件操作解析
- 操作xml文件——解析xml文件
- 使用pull解析器操作xml文件
- dom4j 解析xml文件 的操作
- 使用pull解析器操作xml文件
- XML文件解析&操作&与JSON比较
- 使用pull解析器操作xml文件
- 使用pull解析器操作xml文件
- Spring——jar包详解
- IOS开发之----两种保存用户名和密码实现记住密码库
- [从头学数学] 第15节 认识人民币
- Delphi中调用API函数经验点滴(三)
- nested exception is com.gbase.jdbc.exceptions.jdbc4.GBaseSyntaxErrorException: Mixing of GROUP colum
- C++ XML文件解析操作
- kafka自定定义对象实现
- 数据库
- Apache 虚拟主机的配置
- Python
- iOS怎么防止文件被Icloud或者iTunes备份
- 毫秒值转化为时间
- Mysql 主从
- 山东省第六届蓝桥杯 ///标题:移动距离//c/c++组