Skip to content
Search

TinyXML

lastmod 2023-03-02created 2014-09-197 edits

TinyXML is a small, simple XML parser for the C++ language. It is free and open source software, distributed under the terms of the license of zlib/libpng.

TinyXML is a small, simple C++ XML parser. It is easy to use and easy to port into other programs.

Requirements

The minimum files needed for another project are as follows.

  • tinystr.cpp
  • tinystr.h
  • tinyxml.cpp
  • tinyxml.h
  • tinyxmlerror.cpp
  • tinyxmlparser.cpp

TinyXML-2

TinyXML-2 is a simple, small, efficient, C++ XML parser that can be easily integrating into other programs.

TinyXML-1 served my needs for many years; but it uses memory inefficiently, and doesn't perform as well as desired for mobile devices. I wanted an XML parser that was a little more modern, a little simpler (the "tiny" had been lost a little over the years), and was a good fit for Android.

There are 2 files in TinyXML-2

The two files needed for installation:

  • tinyxml2.cpp
  • tinyxml2.h

Basic usage

Describes basic usage of the TinyXML library. Assume the following XML data exists.

<?xml version="1.0" ?>
<MyApp>
    <!-- Settings for MyApp -->
    <Messages>
        <Welcome>Welcome to MyApp</Welcome>
        <Farewell>Thank you for using MyApp</Farewell>
    </Messages>
    <Windows>
        <Window name="MainFrame" x="5" y="15" w="400" h="250" />
    </Windows>
    <Connection ip="192.168.0.1" timeout="123.456000" />
</MyApp>
  • Reading XML data from a file works as follows.
TiXmlDocument document;
document.LoadFile(_File_Name_); 

Reading XML data from a string works as follows.

TiXmlDocument document;
document.Parse(szXML);

An element can be retrieved as follows.

TiXmlElement * pRoot = document.FirstChildElement("MyApp");

The <Welcome> tag data can be retrieved as follows.

pElement = pRoot->FirstChildElement("Message");
pElement = pElement->FirstChildElement("Welcome");
char * pAA = pElement->Value();  // pAA는  "Welcome" 이다. 
pAA = pElement->GetText();  // pAA는  "Welcome to MyApp" 이다.

The name attribute of the <Windows> tag can be read as follows.

pElement = pRoot->FirstChildElement("Windows");
char * pAA = pElement->Attribute("name");  // pAA에 "MainFrame"이 들어온다. 
int x;
pElement->Attribute("x", &x);  // x 에 숫자 5가 들어온다. 

Iterating over child nodes in a loop works as follows.

pNode = pRoot->FirstChild("sub_node"); 
for (pNode; pNode; pNode = pNode->NextSibling()) {
  pAA = pNode->Value();  
  pElement = pNode->FirstChildElement("item");  
  pAA = pElement->GetText();
}

Saving XML

Build settings or whatever data you want into XML.

  • The declaration is as follows.
TiXmlDocument doc;
TiXmlElement * msg;
TiXmlDeclaration * decl = new TiXmlDeclaration("1.0", "", "");
doc.LinkEndChild(decl);

Doing the above,

<?xml version="1.0" ?>

is generated.

  • Add a sub node as follows.
TiXmlElement * root = new TiXmlElement("MyApp");
doc.LinkEndChild(root);

A TiXmlElement created with new is deleted automatically internally, so there is no need to delete it.

  • Add a comment as follows.
TiXmlComment * comment = new TiXmlComment();
comment->SetValue(" Settings for MyApp " );
root->LinkEndChild(comment);

Use the TiXmlComment class for comments.

Add the Message sub node, its child nodes, and data as follows.

TiXmlElement * msgs = new TiXmlElement("Messages");
root->LinkEndChild(msgs);

msg = new TiXmlElement("Welcome");
msg->LinkEndChild(new TiXmlText("Welcome to MyApp"));
msgs->LinkEndChild(msg);

msg = new TiXmlElement("Farewell");
msg->LinkEndChild(new TiXmlText("Thank you for using MyApp"));
msgs->LinkEndChild(msg);

Adding a node and setting an Attribute works as follows. (Note carefully that it is added under root to match the level.)

TiXmlElement * windows = new TiXmlElement("Windows");
root->LinkEndChild(windows);

TiXmlElement * window;
window = new TiXmlElement("Window");
windows->LinkEndChild(window);
window->SetAttribute("name", "MainFrame");
window->SetAttribute("x", 5);
window->SetAttribute("y", 15);
window->SetAttribute("w", 400);
window->SetAttribute("h", 250);

Setting a Double value (decimal value) works as follows.

TiXmlElement * cxn = new TiXmlElement( "Connection" );
root->LinkEndChild(cxn);
cxn->SetAttribute("ip", "192.168.0.1");
cxn->SetDoubleAttribute("timeout", 123.456);

Save to a file as follows.

doc.SaveFile("text.xml");

Saving to a string works as follows.

TiXmlPrinter printer;
printer.SetStreamPrinting();
Doc.Accept(&printer);
char * pAA = printer.CStr();
std::string str = printer.Str();

Local Download

TinyXML2 3.0.0
Tinyxml2-3.0.0.tar.gz

See also

Favorite site

author Zer0 sitemap