[原][库][c++]tinyxml使用小结

参考:http://blog.csdn.net/L_Andy/article/details/40615517

 

tinyxml官网:

http://www.grinninglizard.com/tinyxml/

官方文档:

http://www.grinninglizard.com/tinyxmldocs/index.html

github上文件下载地址:
https://sourceforge.net/projects/tinyxml/

 

TinyXML类:


TiXmlBase:整个TinyXML模型的基类。
TiXmlAttribute:对应于XML中的元素的属性。
TiXmlNode:对应于DOM结构中的节点。
TiXmlComment:对应于XML中的注释。<!--XXXXX.-->
TiXmlDeclaration:对应于XML中的申明部分,即<?versiong="1.0" ?>。
TiXmlDocument:对应于XML的整个文档。
TiXmlElement:对应于XML的元素。
TiXmlText:对应于XML的文字部分。
TiXmlUnknown:对应于XML的未知部分。
TiXmlHandler:定义了针对XML的一些操作。

 

 

使用:

XML:

<?xml version="1.0" encoding="UTF-8"?>  
<phonebook>  
    <!--one item behalfs one contacted person.-->  
    <item>  
        <name>miaomaio</name>  
    <addr>Shaanxi Xi'an</addr>  
    <tel>13759911917</tel>  
    <email>miaomiao@home.com</email>  
    </item>  
    <item>  
        <name>gougou</name>  
    <addr>Liaoning Shenyang</addr>  
    <tel>15840330481</tel>  
    <email>gougou@home.com</email>  
    </item>  
    <!--more contacted persons.-->  
</phonebook>  

 

C++:

//______________________________________________________________________  
// Read information from xml file.  
  
// define xml file path, as follow , we use relative path,  
// but you can use absolute path also.  
const char* filepath = "phonebookdata.xml";  
TiXmlDocument doc(filepath);  
bool loadOkay = doc.LoadFile();  
// faile to load 'phonebookdata.xml'.  
if (!loadOkay) {      
    printf( "Could not load test file %s. Error='%s'. Exiting.\n", filepath,doc.ErrorDesc() );  
    exit( 1 );  
}  
  
// get dom root of 'phonebookdata.xml', here root should be 'phonebook'.  
TiXmlElement* root = doc.RootElement();  
  
printf("_______________________________________\n\n");  
printf("     contacted person information      \n\n");  
// trace every items below root.  
for( TiXmlNode*  item = root->FirstChild( "item" );  
         item;  
         item = item->NextSibling( "item" ) ) {  
    printf("_______________________________________\n");  
  
    // read name.  
    TiXmlNode* child = item->FirstChild();  
    const char* name = child->ToElement()->GetText();  
    if (name) {  
        printf("name:%s\n",name);  
    } else {  
        printf("name:\n");  
    }  
  
    // read address.  
    child = item->IterateChildren(child);  
    const char* addr = child->ToElement()->GetText();  
    if (addr) {  
        printf("addr:%s\n",addr);  
    } else {  
        printf("addr:\n");  
    }  
  
  
    // read telephone no.  
    child = item->IterateChildren(child);  
    const char* tel = child->ToElement()->GetText();  
        if (tel) {  
        printf("tel:%s\n",tel);  
    } else {  
        printf("tel:\n");  
    }  
  
    // read e-mail.  
    child = item->IterateChildren(child);  
    const char* email = child->ToElement()->GetText();  
    if(email) {  
        printf("email:%s\n",email);  
    } else {  
        printf("email:\n");  
    }  
      
    printf("\n");  
  
}  
//______________________________________________________________________  
  
  
//______________________________________________________________________  
// Add information to xml file and save it.  
TiXmlElement* writeRoot = doc.RootElement();  
TiXmlNode* newNode = new TiXmlElement("item");  
  
   const TiXmlNode* name4NewNode = new TiXmlElement("name");  
newNode->InsertEndChild(*name4NewNode)->InsertEndChild(TiXmlText("pipi"));  
  
const TiXmlNode* addr4NewNode = new TiXmlElement("addr");  
newNode->InsertEndChild(*addr4NewNode)->InsertEndChild(TiXmlText("Shaanxi Xianyang"));  
  
const TiXmlNode* tel4NewNode = new TiXmlElement("tel");  
newNode->InsertEndChild(*tel4NewNode)->InsertEndChild(TiXmlText("02937310627"));  
  
const TiXmlNode* email4NewNode = new TiXmlElement("email");  
newNode->InsertEndChild(*email4NewNode)->InsertEndChild(TiXmlText("pipi@home.com"));  
  
writeRoot->InsertEndChild(*newNode);  
doc.SaveFile();  
//______________________________________________________________________  

 

posted @ 2017-07-18 10:23  南水之源  阅读(371)  评论(0)    收藏  举报