C++处理XML文件
来源:互联网 发布:龙泉驾校网络上课流程 编辑:程序博客网 时间:2024/06/08 07:31
简单说一下使用 TinyXML来解析处理XML(eXtensible Markup Language 可扩展标记语言)文件。
文章转载请注明出处:http://blog.csdn.net/Ouyangxiayun/article/details/78198467
- TinyXML简简介
- TinyXML不能做的 what it doesnt do
- 使用TinyXML的相关工程
- 特性
- 使用STL
- UTF-8
- 打印
- Streams
- White space
- Handles 优化的句柄检验
- Row and Column tracking useful
- How TinyXML works
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
TinyXPath is a small footprint XPath syntax decoder, written in C++.
TinyXML++
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 but 2 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文件.
- xml文件处理经验总结
- DOM处理XML文件
- Ajax处理XML文件
- XJL:XML文件处理
- java 处理xml文件
- 对XML文件处理
- xml 文件处理
- XML文件处理
- js处理xml文件
- python处理xml文件
- c++处理 XML文件
- linq处理xml文件
- python处理XML文件
- xml文件处理
- android XML文件处理
- XML文件处理
- xml文件的处理
- java处理XML文件
- spring框架-第六弹
- unity_NGUI系统学习(十二)_Checkbox单选框的创建
- 静态链表实现
- leetcode_java_第一题 two sum
- Flask项目文件目录
- C++处理XML文件
- 惊喜!Mat研究(1)
- 一道画风清奇的数位dp
- 基于七牛播放器的直播
- File Transfer
- Spring FrameWork学习笔记(一)Spring框架综述
- JDBC部分知识点
- 【完全解决】Ubuntu下正确安装VMware Tools及安装搜狗输入法
- 当你老了,一生最后悔什么?大数据告诉你!