原文链接如下:
http://www.grinninglizard.com/tinyxmldocs/index.html
在程序中使用tinyXML:
To Use in an Application:
使用tinyXML,只需将解压的文件,tinyxml.cpp,tinyxml.h,tinyxmlerror.cpp,tinyxmlparser.cpp,tinystr.cpp,and tinystr.h 加入到你的工程中去,就可以了。它可以编译于任何兼容c++的系统, TinyXML无须开启Exception和RTTI.
Add tinyxml.cpp,and tinystr.h to your project or make file. That’s it! It should compile on any reasonably compliant C++ system. You do not need to enable exceptions or RTTI for TinyXML.
TinyXML是如何工作的
How TinyXML works.
示例如下:
An example is probably the best way to go. Take:
<?xml version="1.0" standalone=no>
<!-- Our to do list data -->
<ToDo>
<Item priority="1"> Go to the <bold>Toy store!</bold></Item>
<Item priority="2"> Do bills</Item>
</ToDo>
看似无关的代码,却是是你读写解析文件的开始:
Its not much of a To Do list,but it will do. To read this file (say “demo.xml”) you would create a document,and parse it in:
TiXmlDocument doc( "demo.xml" );
doc.LoadFile();
加载文件以后,看一下文件中的行语句跟DOM之间的关系。
And its ready to go. Now lets look at some lines and how they relate to the DOM.
<?xml version="1.0" standalone=no>
demo.xml第一行是一个声明,被转化为TiXmlDeclaration 类对象,是demo.xml的第一个子节点。
The first line is a declaration,and gets turned into the TiXmlDeclaration class. It will be the first child of the document node.
这是TinyXML唯一一个特殊处理的标记,此标记存储在TiXmlUnknown类对象中,在写入硬盘时,不要忘了此命令。
This is the only directive/special tag parsed by TinyXML. Generally directive tags are stored in TiXmlUnknown so the commands wont be lost when it is saved back to disk.
<!-- Our to do list data -->
一句注释,将会转化为 TiXmlComment 类对象。
A comment. Will become a TiXmlComment object.
<ToDo>
ToDo标记定义了一个TiXmlElement 对象,此标记没有任何的属性,但包含两个成员。
The “ToDo” tag defines a TiXmlElement object. This one does not have any attributes,but does contain 2 other elements.
<Item priority="1">
创建了一个ToDo成员的子成员。这个成员包含一个属性,名字是”priority”,值是”1”。
Creates another TiXmlElement which is a child of the “ToDo” element. This element has 1 attribute,with the name “priority” and the value “1”.
Go to the
一个TiXmlText对象,是一个叶节点,不能包含其它的子节点。是的 Item成员的孩子。
A TiXmlText. This is a leaf node and cannot contain other nodes. It is a child of the “Item” TiXmlElement.
<bold>
又一个Item成员的子成员。
Another TiXmlElement,this one a child of the “Item” element.
Etc.
回础一下,你得到的整下对象树:
Looking at the entire object tree,you end up with:
TiXmlDocument "demo.xml"
TiXmlDeclaration "version='1.0'" "standalone=no"
TiXmlComment " Our to do list data"
TiXmlElement "ToDo"
TiXmlElement "Item" Attribtutes: priority = 1
TiXmlText "Go to the "
TiXmlElement "bold"
TiXmlText "Toy store!"
TiXmlElement "Item" Attributes: priority=2
TiXmlText "Do bills"