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;
}
}

0 0
原创粉丝点击