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

来源:互联网 发布:小智淘宝外设店网址 编辑:程序博客网 时间:2024/05/20 05:09

废话少说直接上代码,需要的人自然一看便懂,对于第一次接触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

本人郑重声明如下一、本文来自CSDN博客,本文地址http://t.cn/z8iCDPm二、All Rights Reserved. 任何个人或网站转载本文时不得移除本声明.三、不得对文章进行修改,除非明确说明.同时欢迎大家评论转载和分享.