简单说一下使用 TinyXML来解析处理XML(eXtensible Markup Language 可扩展标记语言)文件。
文章转载请注明出处:http://blog.csdn.net/Ouyangxiayun/article/details/78198467
TinyXML简简介
TinyXML是Lee Thomason发布的一个开源库,非常便于使用C++对DOM式的文档进行读、写。他的主页上有对TinyXML的简单介绍。源码可以在Sourceforge上进行下载。
TinyXML库的所有六个文件,即:
- tinystr.h
- tinyxml.h
- tinystr.cpp
- tinyxml.cpp
- tinyxmlerror.cpp
- tinyxmlparser.cpp
只需要将这六个文件添加到工程中,就可以使用TinyXML.
TinyXML不能做的 what it doesn’t do.
TinyXML doesn’t parse or use DTDs (Document Type Definitions) or XSLs (eXtensible Stylesheet Language.)
使用TinyXML的相关工程
TinyXPath is a small footprint XPath syntax decoder, written in C++.
TinyXML++ is a completely new interface to TinyXML that uses MANY of the C++ strengths. Templates, exceptions, and much better error handling.
特性
使用STL
直接在库文件 tinyxml.h 首先加上 “#define TIXML_USE_STL” 即可使用标准库
UTF-8
通常TinyXML会尝试检测并使用源文件中的编码格式,也可以使用 TIXML_DEFAULT_ENCODING宏来强制声明使用的源文件编码格式
打印
TinyXML can print output in several different ways that all have strengths and limitations.
- Print(FILE*)
streamed directl to the FILE object, there’s no memory overhead in the TinyXML code.
used by Print() and SaveFile() - operator <<
integrates with standard C++ iostreams; output in “network printing” mode without line breaks, good for network transmission and moving XML between C++ objects, but hard for a human to read. - TiXMLPrinter
output to a std::string or memory buffer
Streams
#undef TIXML_USE_STL
// tolerant of ill formed XML
C style output:
- based on *FILE
- Print() and SaveFile()
C style input:
- based on *FILE
- Parse() and LoadFile()
#define TIXML_USE_STL
// not tolerant of ill formed XML
C++ style output:
indtended for network transmission rather than readability(maybe slower)
- based on std::ostream
- operator <<
C++ style input: useful for network transmission
- based on std::istream
- operator >>
White space
TiXmlBase::SetCondenseWhileSpace(bool)
在解析XML文件之前应调用 TiXmlBase::SetCondenseWhiteSpace(bool)来设置默认是否压缩空白符,设置好后,一般不用再改动。
Handles 优化的句柄检验
TiXmlElement *root = document.FirstChildElement("Document");
if(root)
{
TiXmlElement *element = root->FirstChildElement("Element");
if(element)
{
TiXmlElement *child = element->FirstChildElement("Child");
if(child)
{
TiXmlElement *child2 = child->NextSiblingElement("Child");
if(child2)
{
// Finally do someting useful....
}
}
}
}
使用TiXmlHandle类,将以上代码优化为:
TiXmlHandle docHandle(&document);
TiXmlElement *child2 = docHandle.FirstChild("Document").FirstChild\
("Element").Child("Child",1).ToElement();
if(child2)
{
// do something useful...
}
Row and Column tracking (useful !!!)
TiXmlBase::Row()
TiXmlBase::Column()
TiXmlDocument::SetTabSize()
How TinyXML works
最直观的使用案例
如下的demo.xml文件:
<?xml version="1.0" standalone=no>
<!-- our to do list data -->
<ToDo>
<Item priority="1">Go to the <bold>Toy store!</bold></Item>
<Item>
Do bills
</Item>
</ToDo>
使用C++代码:
TiXmlDocument doc("demo.xml");
doc.LoadFile();
源文件中的声明 <?xml version="1.0" standalone=no>
自动被处理成TiXmlDeclaration类。It will be the first child ofthe document node.
<!-- our to do list data>
will become a TiXmlComment object.
“ToDo
” tag defines a TiXmlElement object, it has not any attributes but2 other elements.
<Item priority="1">
creates another TiXmlElement which is a child of the “ToDo” element.
Go to the
is a TiXmlText, a child of “Item” TiXmlElement.
<bold>
another TiXmlElement, and is a child of the “Item”element.
更多关于TinyXML的教程可以参见库文件目录/docs/下的tutorial0.html文件.