XML文件解析之Tinyxml(C++)
2018-03-28 15:39
639 查看
TinyXml下载
首先,展示两个Tinyxml资源包的下载链接:其一,TinyXML官方下载链接:https://sourceforge.net/projects/tinyxml/?source=directory
其二,TinyXML2资源GitHub链接:https://github.com/leethomason/tinyxml2
基础函数讲解(源码查看)
以下为TinyXML2函数,TinyXML跟这个差不多类似,如有相似用途,查看源码即可。首先看TinyXML2文件中类组成
- 只是讲解部分我使用过的函数:
class XMLDocument;//xml文件类,类似于C语言里面的FILE,主要用于创建XMLDocument对象,用于打开XML文档 /** Load an XML file from disk. Returns XML_SUCCESS (0) on success, or an errorID. */ XMLError LoadFile( const char* filename );//文件加载,传入文件路径即可 /** Load an XML file from disk. You are responsible for providing and closing the FILE*. NOTE: The file should be opened as binary ("rb") not text in order for TinyXML-2 to correctly do newline normalization. Returns XML_SUCCESS (0) on success, or an errorID. */ XMLError LoadFile( FILE* );//传入文件指针加载文件 class XMLElement;//数据节点 class XMLAttribute;//属性类 //例如:XMLDocument xmlDocument; xmlDocument.LoadFile("./路径"); //Or input your FILE * XMLElement * root = xmlDocument.RootElement();//RootElement();获取该xml文件的根节点 XMLAttribute * curAttribute = curElement->FirstAttribute();//获取属性 //引用一些源码中出现过的注释(比较明确的展示了其用法): //值得注意的是,FirstChildElement()有两种用法,一种是加参数的,即指定子节点名称查找; //还有一种就是不加参数的,即默认查找第一个子节点 //若要获取当前Element的名称可以使用Name()函数 //属性获取方法还有:Element->Attribute("属性名"); //NextSiblingElement();该函数指向兄弟节点 //比如这样获取下一个兄弟节点:curElement = curElement->NextSiblingElement(); /** A XMLHandle is a class that wraps a node pointer with null checks; this is an incredibly useful thing. Note that XMLHandle is not part of the TinyXML-2 DOM structure. It is a separate utility class. Take an example: @verbatim <Document> <Element attributeA = "valueA"> <Child attributeB = "value1" /> <Child attributeB = "value2" /> </Element> </Document> @endverbatim Assuming you want the value of "attributeB" in the 2nd "Child" element, it's very easy to write a *lot* of code that looks like: @verbatim XMLElement* root = document.FirstChildElement( "Document" ); if ( root ) { XMLElement* element = root->FirstChildElement( "Element" ); if ( element ) { XMLElement* child = element->FirstChildElement( "Child" ); if ( child ) { XMLElement* child2 = child->NextSiblingElement( "Child" ); if ( child2 ) { // Finally do something useful. @endverbatim And that doesn't even cover "else" cases. XMLHandle addresses the verbosity of such code. A XMLHandle checks for null pointers so it is perfectly safe and correct to use: @verbatim XMLHandle docHandle( &document ); XMLElement* child2 = docHandle.FirstChildElement( "Document" ).FirstChildElement( "Element" ).FirstChildElement().NextSiblingElement(); if ( child2 ) { // do something useful @endverbatim Which is MUCH more concise and useful. It is also safe to copy handles - internally they are nothing more than node pointers. @verbatim XMLHandle handleCopy = handle; @endverbatim See also XMLConstHandle, which is the same as XMLHandle, but operates on const objects. */ //还有一些我没用过的类 class XMLText; class XMLDeclaration; class XMLPrinter;//相比XMLDocument类给与的打印功能外,它更加强大
接下来是我的一个例子展示
#include <iostream> #include <windows.h> #include <stdio.h> #include "tinyxml2.h" using namespace std; void ReadXML(const tinyxml2::XMLElement * rootElement); int main(void) { tinyxml2::XMLDocument xmlDocument; xmlDocument.LoadFile("./你的文件.xml"); if (xmlDocument.Error()) { cout << "ERROR: XML file not found!" << endl; return 0; } tinyxml2::XMLElement * rootElement = xmlDocument.RootElement(); //Get the first child element to analysis xml file tinyxml2::XMLElement * rootElement1 = rootElement->FirstChildElement("Controls"); //cout << rootElement1->Name() << endl; ReadXML(rootElement1); return 0; } void ReadXML(const tinyxml2::XMLElement * rootElement) { if (!rootElement) { cout << "Root Element is NULL" << endl; return; } const tinyxml2::XMLElement * curElement = rootElement->FirstChildElement(); while (curElement) { if (!strcmp(curElement->Name(), "Label")) { const tinyxml2::XMLAttribute * curAttribute = curElement->FirstAttribute(); const char * tep = curElement->Attribute("InitSize"); cout << tep << endl; while (curAttribute != NULL) { cout << " " << curAttribute->Value(); curAttribute = curAttribute->Next(); } /**/ cout << endl; } else if (!strcmp(curElement->Name(), "DialogButton")) { const tinyxml2::XMLAttribute * curAttribute = curElement->FirstAttribute(); const char * tep = curElement->Attribute("InitSize"); cout << tep << endl; while (curAttribute != NULL) { cout << " " << curAttribute->Value(); curAttribute = curAttribute->Next(); } /**/ cout << endl; } else if (!strcmp(curElement->Name(), "EditBox")) { const tinyxml2::XMLAttribute * curAttribute = curElement->FirstAttribute(); const char * tep = curElement->Attribute("InitSize"); cout << tep << endl; while (curAttribute != NULL) { cout << " " << curAttribute->Value(); curAttribute = curAttribute->Next(); } /**/ cout << endl; } //To check whether the current element has other children element. if (curElement->FirstChildElement()) { //recursion function to analysis the xml file. ReadXML(curElement); } //Get the next element; curElement = curElement->NextSiblingElement(); } }
最后贴上一张有问题的图,主要是为了给大家说一下编码问题
有的编译器可能对于文件读取以及控制台输出默认使用utf-8,但是有些就不一样,比如我的电脑上使用的是VS-2013,但是它的控制台输出即使更改控制台输出格式,对于文字输出依然有问题,所以,如果你只是实验用的话,可以将文件重新保存一下,保存成你需要的文件格式即可其中代码命名为实验使用,无其他意义
相关文章推荐
- C++TinyXml读取解析xml文件
- ***C++解析xml文件
- c++用tinyxml2读xml文件,转化为字节码,解析并保存
- C++ XML解析之TinyXML篇
- C++ tinyXml直接解析XML字符串
- C++ XML解析之TinyXML篇[转]
- tinyxml解析配置文件
- C++ XML解析之TinyXML篇
- C++通过TinyXML类库读写XML文件
- tinyxml非常好的实例 --c++解析XML
- C++中XML的解析与生成之TinyXML
- C++ 使用TinyXML解析XML文件
- C++下利用CMarkup来解析xml文件46
- Linux 下C/C++解析XML文件
- c++xml解析之tinyxml2
- TinyXML(C++ XML解析库)
- C++中用TinyXML对XML文件进行解析
- C++ tinyxml解析小试及VC6.0调试中遇到的问题
- C++的XML解析之TinyXML篇