C + + generating and parsing XML files

Keywords: C++ xml Windows Linux Attribute

1.xml refers to Extensible Markup Language
2.xml is a markup language, similar to html
3.xml is designed to transfer data, not display data
4.xml tags are not predefined. Label needs to be defined by yourself

The difference between XML and HTML

1.xml is not an alternative to html.
2.xml and html are designed for different purposes:
3.xml is designed to transmit and store data, and its focus is the content of data.
4.html is designed to display data, and its focus is the appearance of data.
5.html is designed to display information, while xml is designed to transmit information.

Third party Library

TinyXML is selected as the third-party XML parsing library. TinyXML is also an open-source parsing XML parsing library. It is simple to use, just as its name, for C + + development, and supports Windows and Linux. TinyXML traverses and analyzes XML through DOM model.
Official website address: http://www.grinninglizard.com/tinyxml/

Generate XML file

    TiXmlDocument xmlDocument;
 
    // Add XML declaration
    xmlDocument.LinkEndChild(new TiXmlDeclaration( "1.0", "GBK", "" ));
 
    // Add root element
    TiXmlElement * xmlRoot = new TiXmlElement("root");
    xmlDocument.LinkEndChild(xmlRoot);
 
    //Add child element 1 under root element
    TiXmlElement* xmlChild1 = new TiXmlElement("name");
    xmlRoot->LinkEndChild(xmlChild1);
    xmlChild1->LinkEndChild(new TiXmlText("woniu"));
    xmlChild1->SetAttribute("id", "0001");//set a property
 
 
    //Add child element 2 under root element
    TiXmlElement* xmlChild2 = new TiXmlElement("Student");
    xmlRoot->LinkEndChild(xmlChild2);
 
    TiXmlElement* xmlChild2_01 = new TiXmlElement("name");
    xmlChild2->LinkEndChild(xmlChild2_01);
    xmlChild2_01->LinkEndChild(new TiXmlText("woniu201"));
    
    TiXmlElement* xmlChild2_02 = new TiXmlElement("classes");
    xmlChild2->LinkEndChild(xmlChild2_02);
    xmlChild2_02->LinkEndChild(new TiXmlText("86"));
 
    //Save xml file
    xmlDocument.SaveFile("woniu.xml");

Generate XML as follows:

Parsing XML files

    TiXmlDocument xmlDocument;
    if (!xmlDocument.LoadFile("woniu.xml"))
    {
        return -1;
    }
 
    //Root node
    TiXmlElement* xmlRoot = xmlDocument.RootElement();
    if (xmlRoot == NULL)
    {
        return -1;
    }
 
    //Get child node information 1
    TiXmlElement* xmlNode1Name = xmlRoot->FirstChildElement("name");
    const char* node1Name = xmlNode1Name->GetText();
    const char* node1AttId = xmlNode1Name->Attribute("id");
 
    //Get child node information 2
    TiXmlElement* xmlNode2Stu = xmlRoot->FirstChildElement("Student");
    TiXmlElement* xmlNode2_name = xmlNode2Stu->FirstChildElement("name");
    TiXmlElement* xmlname2_classes = xmlNode2Stu->FirstChildElement("classes");
    const char* node2Name = xmlNode2_name->GetText();
    const char* node2Classes = xmlname2_classes->GetText();

Follow the public number below and reply "101" to get the source code

Posted by expostfacto on Wed, 16 Oct 2019 16:05:44 -0700