C++ & XML(TinyXML)

Using C++ to create and manipulate XML files (study purpose)

C++ and TinyXML

Simple example of a library system

Using a simple example of a library system to manipulate data using XML files TinyXML it’s a library used in this context.

XML

What is XML? How to use it with the programming languages?

  • XML stands for Extensible Markup Language. It is a markup language that defines rules for encoding documents in a format that is both human-readable and machine-readable.

  • XML can be used with programming languages to store and transport data between systems. It provides a structured way to represent and exchange information.

Example

<bookstore>
  <book category="cooking">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>
</bookstore>

Code Example

#include <iostream>
#include <string>
#include "tinyxml2.h"

using namespace tinyxml2;

int main() {
  XMLDocument doc;
  doc.LoadFile("example.xml");

  XMLElement* root = doc.FirstChildElement("bookstore");
  if (root != nullptr) {
    XMLElement* book = root->FirstChildElement("book");
    if (book != nullptr) {
      XMLElement* price = book->FirstChildElement("price");
      if (price != nullptr) {
        price->SetText("35.00");
        doc.SaveFile("example.xml");
        std::cout << "Price updated successfully!" << std::endl;
      }
    }
  }

  return 0;
}

Link of the project with a more concrete example

References

And the explanation about XML

0%