Home  >  Article  >  Backend Development  >  XML processing skills in C++

XML processing skills in C++

王林
王林Original
2023-08-21 21:51:331383browse

XML is a commonly used data exchange format and is widely used in networks, databases, configuration files and other fields. In C, XML processing can be completed through third-party libraries, such as TinyXML, RapidXML, Boost.PropertyTree, etc. This article will introduce techniques for using the TinyXML library to process XML files.

  1. Installation and configuration TinyXML
    TinyXML is a lightweight XML parsing library, the latest version can be downloaded at https://sourceforge.net/projects/tinyxml/files/tinyxml/. After downloading, unzip it, put the two files tinyxml.h and tinyxml.cpp into the source code directory of the project, and configure them accordingly in the project.
  2. Loading XML files
    When using the TinyXML library, we need to use the TiXmlDocument class to represent the XML file and use the LoadFile() method to load the file. Here is a simple example:
#include "tinyxml.h"
#include <iostream>
using namespace std;

int main()
{
    TiXmlDocument xmlDocument;
    if (xmlDocument.LoadFile("example.xml"))
    {
        cout << "文件加载成功!" << endl;
    }
    else
    {
        cout << "文件加载失败,请检查文件路径是否正确。" << endl;
    }
    return 0;
}
  1. Traversing XML nodes
    XML file can be viewed as a tree structure, where each node represents an element or attribute. The TinyXML library provides the TiXmlElement class and TiXmlAttribute class to represent XML elements and attributes respectively. We can use FirstChildElement(), NextSiblingElement(), FirstChild(), NextSibling() and other methods to traverse XML nodes. The following is an example of traversing an XML file:
#include "tinyxml.h"
#include <iostream>
using namespace std;

int main()
{
    TiXmlDocument xmlDocument;
    if (xmlDocument.LoadFile("example.xml"))
    {
        TiXmlElement* rootElement = xmlDocument.RootElement();
        for (TiXmlElement* element = rootElement->FirstChildElement(); element != nullptr; element = element->NextSiblingElement())
        {
            cout << "元素名称:" << element->Value() << endl;
            for (TiXmlAttribute* attribute = element->FirstAttribute(); attribute != nullptr; attribute = attribute->Next())
            {
                cout << "属性名称:" << attribute->Name() << ",属性值:" << attribute->Value() << endl;
            }
        }
    }
    else
    {
        cout << "文件加载失败,请检查文件路径是否正确。" << endl;
    }
    return 0;
}
  1. Update XML node
    We can use the SetValue() method to modify the value of the node, and the SetAttribute() method to modify the node's value. Attributes. The following is an example of updating an XML file:
#include "tinyxml.h"
#include <iostream>
using namespace std;

int main()
{
    TiXmlDocument xmlDocument;
    if (xmlDocument.LoadFile("example.xml"))
    {
        TiXmlElement* element = xmlDocument.RootElement()->FirstChildElement("person")->FirstChildElement("name");
        element->SetValue("John Smith");
        TiXmlAttribute* attribute = element->FirstAttribute("lang");
        attribute->SetValue("en");
        xmlDocument.SaveFile("example.xml");
        cout << "更新成功!" << endl;
    }
    else
    {
        cout << "文件加载失败,请检查文件路径是否正确。" << endl;
    }
    return 0;
}
  1. Creating XML Nodes
    We can use the constructor of the TiXmlElement class to create new XML elements. Use the LinkEndChild() method to insert a new element into a child node of the parent element. The following is an example of creating an XML file:
#include "tinyxml.h"
#include <iostream>
using namespace std;

int main()
{
    TiXmlDocument xmlDocument;
    TiXmlElement* rootElement = new TiXmlElement("root");
    TiXmlElement* personElement = new TiXmlElement("person");
    TiXmlElement* nameElement = new TiXmlElement("name");
    nameElement->SetValue("Tom");
    nameElement->SetAttribute("lang", "en");
    personElement->LinkEndChild(nameElement);
    rootElement->LinkEndChild(personElement);
    xmlDocument.LinkEndChild(rootElement);
    xmlDocument.SaveFile("example.xml");
    cout << "创建成功!" << endl;
    return 0;
}

In addition to the above operations, the TinyXML library also provides other methods to operate XML files, such as deleting nodes, querying nodes, etc. Using the TinyXML library can make XML processing in C easy and simple, and improve development efficiency.

The above is the detailed content of XML processing skills in C++. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn