The use of tinyxml in vc++

TinyXML is an open source parsing library for parsing XML, which can be used in C++ and can be compiled in Windows or Linux. Using TinyXML for C++ XML parsing, it is simple to use and easy to use.
The model of this parsing library parses the XML file and then generates a DOM model in memory, so that we can easily traverse the XML tree. 
The DOM model is the Document Object Model, which divides the entire document into multiple elements (such as books, chapters, sections, paragraphs, etc.), and uses a tree structure to represent the order relationship and nested inclusion relationship between these elements.

Introduction to TinyXML

In TinyXML, some classes are defined according to various elements of XML:
TiXmlBase: The base class of the entire TinyXML model.
TiXmlAttribute: corresponds to an attribute of an element in XML.
TiXmlNode: corresponds to a node in the DOM structure.
TiXmlComment: Corresponds to comments in XML.
TiXmlDeclaration: corresponds to the declaration part in XML, that is, <? versiong="1.0" ?>.
TiXmlDocument: corresponds to the entire document in XML.
TiXmlElement: Element corresponding to XML.
TiXmlText: Corresponds to the text part of XML.
TiXmlUnknown: Corresponds to an unknown part of XML. 
TiXmlHandler: defines some operations for XML.

Download and compile

The running environment of this article is: Redhat 5.5 + g++version 4.6.1 + GNU Make 3.81 + tinyxml_2_6_2

The download address is: http://sourceforge.net/projects/tinyxml/

After decompression, the folder tinyxml is obtained, and the header files and cpp files of tinyxml are all in this folder. In order to manage our project, we still organize tinyxml.

Since tinyxml not only supports Linux compilation, but also supports compilation under Windows, so after decompression, there are not only h files, cpp files, but also some project files of the vc project. Here we only use it on Linux, so only h files and cpp are left. file, all other files will be deleted

Here I list the sorted working directory:

copy code
tinyxml/            // Working directory
 |-- include         // Header file root directory
 | |-- tinyxml     // tinyxml header file, including tinystr.h tinyxml.h
 |-- src             // cpp source file root directory
  |-- tinyxml        / / tinyxml source folder, including tinystr.cpp tinyxml.cpp tinyxmlerror.cpp tinyxmlparser.cpp
  |-- main.cpp    // our main function, the sample code that calls tinyxml  
 |-- conf             // the xml file used in our example The folder where it is located
 |-- makefile        // makefile, we don't need to say more, if you don't understand, please see the makefile best practice on my blog
copy code

 

Simplest example

我们在conf目录下建立student.xml,xml代码如下:

copy code
<School name="软件学院">  
    <Class name = "C++">  
        <Student name="tinyxml" number="123">  
            <email>tinyxml@163.com</email>  
            <address>中国</address>           
        </Student>  
        <Student name="jsoncpp" number="456">  
            <email>[email protected]</email>  
            <address>美国</address>           
        </Student>  
    </Class>  
</School>  
copy code

使用tinyxml,我们只需要在头文件中包含 <tinyxml.h>就行了。

打印整个XML代码如下:

copy code
void printSchoolXml() {
    using namespace std;
    TiXmlDocument doc;  
    const char * xmlFile = "conf/school.xml";   
    if (doc.LoadFile(xmlFile)) {    
        doc.Print();  
    } else {
        cout << "can not parse xml conf/school.xml" << endl;
    }   
}
copy code

 

读取XML

代码如下:

copy code
void readSchoolXml() {
    using namespace std;
    const char * xmlFile = "conf/school.xml";
    TiXmlDocument doc;                              
    if (doc.LoadFile(xmlFile)) {
        doc.Print();
    } else {
        cout << "can not parse xml conf/school.xml" << endl;
        return;
    }
    TiXmlElement* rootElement = doc.RootElement();  //School元素  
    TiXmlElement* classElement = rootElement->FirstChildElement();  // Class元素
    TiXmlElement* studentElement = classElement->FirstChildElement();  //Students  
    for (; studentElement != NULL; studentElement = studentElement->NextSiblingElement() ) {
        TiXmlAttribute* attributeOfStudent = studentElement->FirstAttribute();  //获得student的name属性  
        for (;attributeOfStudent != NULL; attributeOfStudent = attributeOfStudent->Next() ) {
            cout << attributeOfStudent->Name() << " : " << attributeOfStudent->Value() << std::endl;       
        }                                 

        TiXmlElement* studentContactElement = studentElement->FirstChildElement();//获得student的第一个联系方式 
        for (; studentContactElement != NULL; studentContactElement = studentContactElement->NextSiblingElement() ) {
            string contactType = studentContactElement->Value();
            string contactValue = studentContactElement->GetText();
            cout << contactType  << " : " << contactValue << std::endl;           
        }   
     
    } 
}
copy code

写入xml

这里我们将通过xml写入代码操作,写入几乎和conf/school.xml同样内容到conf/school-write.xml,代码如下:

copy code
void writeSchoolXml() {
    using namespace std;
    const char * xmlFile = "conf/school-write.xml"; 
    TiXmlDocument doc;  
    TiXmlDeclaration * decl = new TiXmlDeclaration("1.0", "", "");  
    TiXmlElement * schoolElement = new TiXmlElement( "School" );  
    TiXmlElement * classElement = new TiXmlElement( "Class" );  
    classElement->SetAttribute("name", "C++");

    TiXmlElement * stu1Element = new TiXmlElement("Student");
    stu1Element->SetAttribute("name", "tinyxml");
    stu1Element->SetAttribute("number", "123");
    TiXmlElement * stu1EmailElement = new TiXmlElement("email");
    stu1EmailElement->LinkEndChild(new TiXmlText("[email protected]") );
    TiXmlElement * stu1AddressElement = new TiXmlElement("address");
    stu1AddressElement->LinkEndChild(new TiXmlText("中国"));
    stu1Element->LinkEndChild(stu1EmailElement);
    stu1Element->LinkEndChild(stu1AddressElement);

    TiXmlElement * stu2Element = new TiXmlElement("Student");
    stu2Element->SetAttribute("name", "jsoncpp");
    stu2Element->SetAttribute("number", "456");
    TiXmlElement * stu2EmailElement = new TiXmlElement("email");
    stu2EmailElement->LinkEndChild(new TiXmlText("[email protected]"));
    TiXmlElement * stu2AddressElement = new TiXmlElement("address");
    stu2AddressElement->LinkEndChild(new TiXmlText("美国"));
    stu2Element->LinkEndChild(stu2EmailElement);
    stu2Element->LinkEndChild(stu2AddressElement);

    classElement->LinkEndChild(stu1Element);  
    classElement->LinkEndChild(stu2Element);  
    schoolElement->LinkEndChild(classElement);  
    
    doc.LinkEndChild(decl);  
    doc.LinkEndChild(schoolElement);
    doc.SaveFile(xmlFile);  
}
copy code

 

Download project

click to download

Compile and run steps after download

unzip tinyxml.zip
cd tinyxml
make
./main

 

More actions

  Please refer to  http://www.grinninglizard.com/tinyxmldocs/tutorial0.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325904272&siteId=291194637