c++ 解析xml的小工具tinyxml

来源:互联网 发布:rbac java 编辑:程序博客网 时间:2024/05/07 15:32

例子:

person.xml

  1 <Persons>
  2     <Person ID="1">
  3         <name>phinecos</name>
  4         <age>22</age>
  5     </Person>
  6 </Persons>

 

c++测试代码:

  1 #include <iostream>
  2 #include "tinyxml.h"
  3 #include "tinystr.h"
  4 #include <string>
  5
  6 using namespace std;
  7
  8
  9 int main(){
 10         TiXmlDocument* myDocument = new TiXmlDocument();
 11         myDocument->LoadFile("person.xml");
 12         TiXmlElement* rootElement = myDocument->RootElement();
 13         cout << rootElement->Value() << endl;
 14         TiXmlElement* Person = rootElement->FirstChildElement();
 15         if(Person){
 16                 cout << "person exists"<<endl;
 17                 TiXmlAttribute *personAttributes = Person->FirstAttribute();
 18                 cout << personAttributes->Name()<<":"<<personAttributes->Value()<<endl;
 19         }
 20 }

 

其实就是每次申明该类对象的指针,然后,用其他的函数使指针指向下一个对象。但是如果知道xml中的某个key,要直接取value好像不行。需要遍历一次,然后比对才能拿到。

 

不过至少可以用了,而且比较简单。不错~

原创粉丝点击