• C++库(TinyXML)


    C++库(TinyXML)

    什么是XML?

      “当 XML(扩展标记语言)于 1998 年 2 月被引入软件工业界时,它给整个行业带来了一场风暴。有史以来第一次,这个世界拥有了一种用来结构化文档和数据的通用且适应性强的格式,它不仅仅可以用于 WEB,而且可以被用于任何地方。”

      XML 指扩展标记语言,是一种信息交换的标准。

    <!-- a xml simple example-->
    <Persons>
            <Person ID="1">
                <name>Kobe</name>
                <age>24</age>
            </Person>
            <Person ID="2">
                <name>Michael</name>
                <age>23</age>
           </Person>
     </Persons>

    什么是TinyXML?

      当前,对xml的使用非常广泛,读取和设置xml配置文件是我们最常用的操作。常见C/C++ XML解析器有Tinyxml、XERCES、squashxml、xmlite、pugxml、libxml等等,这些解析器有些是支持多语言的,有些只是单纯C/C++的。

      TinyXML是目前非常流行的一款基于DOM模型的XML解析器,简单易用且小巧玲珑,非常适合存储简单数据,配置文件,对象序列化等数据量不是很大的操作。这个解析库的模型通过解析XML文件,然后在内存中生成DOM模型,从而让我们很方便的遍历这棵XML树。

    下载安装TinyXML

      下载https://sourceforge.net/projects/tinyxml/

          安装:解压缩tinyXML后,将这六个文件添加到你的C++工程中,分别是tinystr.h、tinystr.cpp、tinyxml.h、tinyxml.cpp、tinyxmlerror.cpp、tinyxmlparser.cpp。在需要操作xml文件的地方,使用如下代码,就可以引入TinyXML类库。

    #include<tinyxml>   或 #include "tinyxml.h"

    TinyXML类结构 

      TiXmlBase:整个TinyXML模型的基类。

      TiXmlAttribute:对应于XML中的元素的属性。

      TiXmlNode:对应于DOM结构中的节点。

      TiXmlComment:对应于XML中的注释

      TiXmlDeclaration:对应于XML中的申明部分,即<?versiong="1.0" ?>。

      TiXmlDocument:对应于XML的整个文档。

      TiXmlElement:对应于XML的元素。

      TiXmlText:对应于XML的文字部分

      TiXmlUnknown:对应于XML的未知部分。

      TiXmlHandler:定义了针对XML的一些操作。

     

    一个XML读写例子

     1 #include <iostream>
     2 #include "tinyxml.h"
     3 #include "tinystr.h"
     4 #include <string>
     5 #include <windows.h>
     6 #include <atlstr.h>
     7 
     8 using namespace std;
     9 
    10 CString GetAppPath()
    11 {
    12     TCHAR modulePath[MAX_PATH];
    13     GetModuleFileName(NULL, modulePath, MAX_PATH);
    14     CString strModulePath(modulePath);
    15     strModulePath = strModulePath.Left(strModulePath.ReverseFind(_T('\')));
    16     return strModulePath;
    17 }
    18 
    19 bool CreateXmlFile(string& szFileName)
    20 {
    21     try
    22     {
    23         TiXmlDocument *myDocument = new TiXmlDocument();
    24         TiXmlElement *RootElement = new TiXmlElement("Persons");
    25         myDocument->LinkEndChild(RootElement);
    26         TiXmlElement *PersonElement = new TiXmlElement("Person");
    27         RootElement->LinkEndChild(PersonElement);
    28         PersonElement->SetAttribute("ID", "1");
    29         TiXmlElement *NameElement = new TiXmlElement("name");
    30         TiXmlElement *AgeElement = new TiXmlElement("age");
    31         PersonElement->LinkEndChild(NameElement);
    32         PersonElement->LinkEndChild(AgeElement);
    33         TiXmlText *NameContent = new TiXmlText("Michael");
    34         TiXmlText *AgeContent = new TiXmlText("23");
    35         NameElement->LinkEndChild(NameContent);
    36         AgeElement->LinkEndChild(AgeContent);
    37         CString appPath = GetAppPath();
    38         string seperator = "\";
    39         string fullPath = appPath.GetBuffer(0) +seperator+szFileName;
    40         myDocument->SaveFile(fullPath.c_str());
    41     }
    42     catch (string& e)
    43     {
    44         return false;
    45     }
    46     return true;
    47 }
    48 
    49 bool ReadXmlFile(string& szFileName)
    50 {
    51     try
    52     {
    53         CString appPath = GetAppPath();
    54         string seperator = "\";
    55         string fullPath = appPath.GetBuffer(0) +seperator+szFileName;
    56         TiXmlDocument *myDocument = new TiXmlDocument(fullPath.c_str());
    57         myDocument->LoadFile();
    58         TiXmlElement *RootElement = myDocument->RootElement();
    59         cout << RootElement->Value() << endl;
    60         TiXmlElement *FirstPerson = RootElement->FirstChildElement();
    61         TiXmlElement *NameElement = FirstPerson->FirstChildElement();
    62         TiXmlElement *AgeElement = NameElement->NextSiblingElement();
    63         TiXmlAttribute *IDAttribute = FirstPerson->FirstAttribute();
    64         cout << NameElement->FirstChild()->Value() << endl;
    65         cout << AgeElement->FirstChild()->Value() << endl;
    66         cout << IDAttribute->Value()<< endl;
    67     }
    68     catch (string& e)
    69     {
    70         return false;
    71     }
    72     return true;
    73 }
    74 
    75 int main(void)
    76 {
    77     string fileName = "info.xml";
    78     CreateXmlFile(fileName);
    79     ReadXmlFile(fileName);
    80 }
  • 相关阅读:
    java内存泄露
    hbase java api
    配置CRT远程登录
    kafka分区消费模型
    JAVA内存区域
    JVM分代和垃圾回收
    spring中bean的作用域
    分布式RPC
    session共享
    ZooKeeper实现分布式session
  • 原文地址:https://www.cnblogs.com/MakeView660/p/6038173.html
Copyright © 2020-2023  润新知