C++解析XML


1.介紹

  讀取和設置xml配置文件是最常用的操作,TinyXML是一個開源的解析XML的C++解析庫,能夠在Windows或Linux中編譯。這個解析庫的模型通過解析XML文件,然后在內存中生成DOM模型,從而讓我們很方便的遍歷這棵XML樹。  

  下載TinyXML的網址:http://www.grinninglizard.com/tinyxml/

  使用TinyXML只需要將其中的6個文件拷貝到項目中就可以直接使用了,這六個文件是:tinyxml.h、tinystr.h、tinystr.cpp、tinyxml.cpp、tinyxmlerror.cpp、tinyxmlparser.cpp。

原文鏈接:https://www.cnblogs.com/xudong-bupt/p/3733306.html#undefined
XML用的人挺多,所以我也想看下怎么用C++解析生成XML。
准備用C++&GDIPlus仿個愛奇藝圖片查看功能,發現自繪界面的話,各個坐標太多太亂了。
可以考慮將其坐標事先寫到XML中,然后運行時將其讀取出來,也方便以后的改寫。

下面是我抄過來加了注釋的代碼:

  1 #include <iostream>
  2 #include "tinyxml.h"
  3 #include <string>
  4 
  5 using namespace std;
  6 
  7 enum SuccessEnum {FAILURE,SUCCESS};
  8 
  9 SuccessEnum loadXML(void)
 10 {
 11     // XML文檔
 12     TiXmlDocument doc;
 13     // 加載XML文檔
 14     if (!doc.LoadFile("a.xml"))
 15     {
 16         cerr << doc.ErrorDesc() << endl;
 17         return FAILURE;
 18     }
 19 
 20     // 定義根節點變量並賦值為文檔的第一個根節點
 21     TiXmlElement *root = doc.FirstChildElement();
 22     // 如果沒有找到根節點,說明是空XML文檔或者非XML文檔
 23     if (root == NULL)
 24     {
 25         cerr << "Failed to load file: No root element." << endl;
 26         // 清理內存
 27         doc.Clear();
 28         return FAILURE;
 29     }
 30 
 31     // 遍歷子節點
 32     for (TiXmlElement *elem = root->FirstChildElement(); elem != NULL; elem=elem->NextSiblingElement())
 33     {
 34         // 獲取元素名
 35         string elemName = elem->Value();
 36         const char *attr;
 37         // 獲取元素屬性值
 38         attr = elem->Attribute("priority");
 39         if (strcmp(attr,"1") == 0)
 40         {
 41             // 還有子節點bold,bold下還有一段文本
 42             TiXmlElement *e1 = elem->FirstChildElement("bold");
 43             // bold下只有一段文本元素,所以,只獲取一次子元素就好
 44             TiXmlNode *e2 = e1->FirstChild();
 45             // 輸出子文本元素值
 46             cout << "priority=1\t" << e2->ToText()->Value() << endl;
 47         }
 48         else if (strcmp(attr,"2") == 0)
 49         {
 50             // 屬性值為"2"的下面只有一段文本
 51             TiXmlNode *e1 = elem->FirstChild();
 52             // 取出來並輸出
 53             cout << "priority=2\t" << e1->ToText()->Value() << endl;
 54         }
 55     }
 56     // 清理內存
 57     doc.Clear();
 58     return SUCCESS;
 59 }
 60 
 61 SuccessEnum saveXML(void)
 62 {
 63     // XML文檔
 64     TiXmlDocument doc;
 65 
 66     // 根節點(根節點名字隨意)
 67     TiXmlElement *root = new TiXmlElement("root");
 68     // 在文檔中加入一個根節點
 69     doc.LinkEndChild(root);
 70     
 71     // 第一個元素
 72     TiXmlElement *element1 = new TiXmlElement("Element1");
 73     // 在根節點下加入子元素
 74     root->LinkEndChild(element1);
 75     // 添加子元素屬性
 76     element1->SetAttribute("attribute1","some value");
 77 
 78     // 第二個元素
 79     TiXmlElement *element2 = new TiXmlElement("Element2");
 80     // 在根節點下再次加入元素
 81     root->LinkEndChild(element2);
 82     // 設置該元素第一個屬性值
 83     element2->SetAttribute("attribute2","2");
 84     // 設置該元素第二個屬性值
 85     element2->SetAttribute("attribute3","3");
 86 
 87     // 第三個元素
 88     TiXmlElement *element3 = new TiXmlElement("Element3");
 89     // 注意:這是在第二個元素(element2)下加入元素(element3)
 90     element2->LinkEndChild(element3);
 91     // 設置該元素屬性值
 92     element3->SetAttribute("attribute4","4");
 93 
 94     // 文本元素
 95     TiXmlText *text = new TiXmlText("Some text.");
 96     // 在第二個元素下加入文本
 97     element2->LinkEndChild(text);
 98 
 99     // 保存文檔
100     bool sucess = doc.SaveFile("b.xml");
101     // 清理內存
102     doc.Clear();
103 
104     if (sucess)
105         return SUCCESS;
106     else
107         return FAILURE;
108 }
109 
110 int main(void)
111 {
112     cout << "Hello XML!" << endl;
113 
114     if (loadXML() == FAILURE)
115         cout << "XML 加載失敗!" << endl;
116     else
117         cout << "XML 加載成功!" << endl;
118 
119     if (saveXML() == FAILURE)
120         cout << "XML 保存失敗!" << endl;
121     else
122         cout << "XML 保存成功!" << endl;
123 
124     return 0;
125 }

 項目中用到的a.xml文檔:

 1 <ToDo>
 2     <Item priority="1"> 
 3         <bold>
 4             Book store!
 5         </bold>
 6     </Item>
 7     <Item priority="2"> 
 8         book1
 9     </Item>
10     <Item priority="2"> 
11         book2
12     </Item>
13 </ToDo>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM