TinyXml1

Tinyxml 有多种输出方式,他们有各自的优点和缺点

Print(FILE*) 作为标准C输出,包含了c的标准输出文件

  • 你不能控制打印选项
  • 直接输出到文件对象,所以没有内存开销
  • 使用Print()和SaveFile()

operator<< 输出到C++ 流

  • 使用标准的C++ iostreams

TixmlPrinter 输出到std::string 或者内存buffer

  • API 更简明
  • 为了版本完善和扩展它

Streams

tinyxml 使用 TIXML_USE_STL 支持c++ streams(<<,>>) 和c的Steams 一样方便,但是有一些不同的地方需要注意:

c style output:

  • 基于 FILE*
  • 使用Print() 和SaveFile() 方法

生成格式化的输出,为了让人更方便阅读.

c style input:

  • 基于 FILE*
  • 使用Parse()和LoadFile()函数

方便阅读,不需要使用C++ Streams

c++ style output:

  • 基于 std::ostream
  • 操作符<<(operator<<)

c++ style input:

  • 基于std::instream
  • operator>>

安装使用

linux 使用Makefile,Windows 使用 vc++ .dsw 文件

vc6工程说明:

tinyxml:tinyxml lib non-STL

tinyxmlSTL: tinyxml library STL

tinyxmlTest: testapp non-STL

tinyxmlTestSTL: testapp STL

TinyXml 如何工作

从一个简单的例子开始

<?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>

创建一个文档,解析

TiXmlDocument doc( "demo.xml" );
doc.LoadFile();
<?xml version="1.0" standalone=no>

第一行是定义,会返回一个TiXmlDeclaration 类,他是文档的第一个子节点

<!-- Our to do list data -->

注释行.会生成一个TiXmlComment 对象


<ToDo>

 "ToDo" 标签 定义了一个 TiXmlElement 对象. 他没有任何属性,但是包含了2个元素.

<Item priority="1">

 创建另外一个 TiXmlElement 对象作为"ToDo" 元素.这个元素有一个属性,属性名字是 "priority" and the value "1".

  go to the

一个 TiXmlText对象.他是一个叶子节点,不在包含子节点.他是 "Item"(TiXmlElement)的子节点

<bold>
另外一个 TiXmlElement 对象, 他也是 "Item" 元素子节点

遍历这个树,可以得到以下信息

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"
原文地址:https://www.cnblogs.com/fjchenqian/p/2738984.html