Skip to content

Latest commit

 

History

History
99 lines (74 loc) · 3.68 KB

tinyxml2.md

File metadata and controls

99 lines (74 loc) · 3.68 KB

TinyXML-2 is a simple, small, and efficient XML parser and generator for C++. It is designed to be lightweight, easy to use, and easy to integrate into your C++ projects. TinyXML-2 is often used for loading and saving XML documents, which is a common task in many applications like configuration management, data serialization, and more.

Key Features of TinyXML-2:

  • Lightweight: TinyXML-2 is designed to be small and efficient, making it ideal for use in resource-constrained environments.
  • Simple API: It provides a straightforward API for parsing, navigating, and creating XML documents.
  • No Dependencies: It is a standalone library, meaning it doesn't rely on other libraries, making it easier to integrate into your projects.

Basic Concepts:

  • XMLDocument: The main class used to load, parse, and save XML files.
  • XMLElement: Represents an element in the XML document. It can contain other elements, attributes, and text.
  • XMLAttribute: Represents an attribute in an XML element.
  • XMLText: Represents the text content within an XML element.

Example of Using TinyXML-2 in C++

Here's a simple example to demonstrate how to use TinyXML-2 to parse an XML file and access its contents:

  1. Include TinyXML-2: First, you need to include the TinyXML-2 header file in your code.
#include "tinyxml2.h"
#include <iostream>

using namespace tinyxml2;

int main() {
    // Create an XMLDocument object
    XMLDocument doc;

    // Load an XML file into the document
    XMLError eResult = doc.LoadFile("example.xml");
    if (eResult != XML_SUCCESS) {
        std::cout << "Failed to load file" << std::endl;
        return eResult;
    }

    // Access the root element
    XMLElement* root = doc.RootElement();
    if (root == nullptr) {
        std::cout << "Failed to load root element" << std::endl;
        return XML_ERROR_PARSING_ELEMENT;
    }

    // Print the name of the root element
    std::cout << "Root element: " << root->Name() << std::endl;

    // Access a child element
    XMLElement* childElement = root->FirstChildElement("ChildElement");
    if (childElement != nullptr) {
        const char* childText = childElement->GetText();
        if (childText != nullptr) {
            std::cout << "Child element text: " << childText << std::endl;
        }
    }

    // Save the modified XML document
    doc.SaveFile("output.xml");

    return 0;
}
  1. Example XML File (example.xml):
<?xml version="1.0"?>
<Root>
    <ChildElement>Some text here</ChildElement>
</Root>

Explanation:

  1. Loading the XML File:

    • The doc.LoadFile("example.xml") function loads the XML file example.xml into the XMLDocument object doc.
    • XML_SUCCESS is checked to ensure that the file loaded correctly.
  2. Accessing the Root Element:

    • doc.RootElement() returns a pointer to the root element of the XML document.
    • We check if the root element is nullptr to ensure it was found.
  3. Accessing Child Elements:

    • root->FirstChildElement("ChildElement") accesses the first child element with the tag name ChildElement.
    • The GetText() function retrieves the text content inside the element.
  4. Saving the XML Document:

    • After potentially modifying the document, doc.SaveFile("output.xml") saves the changes back to a file.

Output:

If you run the above code with the example.xml file, it should print:

Root element: Root
Child element text: Some text here

This example demonstrates the basic usage of TinyXML-2 for loading, accessing, and saving XML documents. You can expand upon this by adding more elements, attributes, and handling different types of XML data.

source