您的位置:首页 > 其它

用TinyXml2读取XML文件的一个简单Demo

2013-09-06 21:37 316 查看
废话少说直接上代码,需要的人自然一看便懂,对于第一次接触TinyXml2的人来说还是有帮助的.

<?xml version="1.0"?>
<Table name="PersonInfo">
<Person Type="学生">
<Age age = "年龄">18</Age>
<Height Hei = "身高">1.7</Height>
</Person>
<Person Type="教师">
<Age age = "年龄">28</Age>
<Height Hei = "身高">1.6</Height>
</Person>
<Person Type="警察">
<Age age = "年龄">30</Age>
<Height Hei = "身高">1.8</Height>
</Person>
</Table>

tinyxml2::XMLDocument Doc;
Doc.LoadFile("Test.xml");
tinyxml2::XMLElement *pRoot=Doc.RootElement();//获取根节点
tinyxml2::XMLElement *pNode=pRoot->FirstChildElement("Person");
while (pNode)
{
tinyxml2::XMLElement *pChildNode=pNode->FirstChildElement();//获取第一个值为Value的子节点 默认返回第一个子节点
const char* pContent;
const tinyxml2::XMLAttribute *pAttributeOfNode = pNode->FirstAttribute();//获取第一个属性值
std::cout<< pAttributeOfNode->Value()<<":";
while(pChildNode)
{
pContent=pChildNode->GetText();
std::cout<<pChildNode->FirstAttribute()->Value()<<":"<<pContent<<" ";
pChildNode=pChildNode->NextSiblingElement();
}
std::cout<<std::endl;
pNode=pNode->NextSiblingElement();
}

程序运行结果如下:

学生:年龄:18 身高:1.7
教师:年龄:28 身高:1.6
警察:年龄:30 身高:1.8
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: