3/05/2014

(C, C++) TinyXML , xml read & write

Firstly, download TinyXML source code on here -> http://sourceforge.net/projects/tinyxml/

After unzip, you can see many files and folders like that.

But, we only need 6 files.  
Copy these files to appropriate folder.

Make your project and add 6 files in your project.


This is example of xml file. Save file name to "ex4.xml"

---
< ? xml version="1.0" ?>
< LibraryIndex>
    < !-- 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" />
< /LibraryIndex>

----

Make code like this and run.
In my case, 6 TinyXML files are located "./tinyXML/"

---
#include < stdio.h>
#include "./tinyXML/tinyxml.h"


void main()
{
 TiXmlDocument doc;
 bool loadOkay = doc.LoadFile("ex4.xml");

 if (loadOkay)
 {
  printf("open success\n");
 }
 else
 {
  printf("Failed to load file \n");
 } 
}
---

If you see the "open success" massage, xml file is opened nomally.


-----------------------------
XML read & parsing example source code.
---
#include < stdio.h>
#include "./tinyXML/tinyxml.h"


void main()
{
 TiXmlDocument doc;
 bool loadOkay = doc.LoadFile("ex4.xml");

 if (loadOkay)
 {
  printf("open success\n");
 }
 else
 {
  printf("Failed to load file \n");
 } 


 //root node access
 TiXmlElement* pRoot = doc.FirstChildElement("LibraryIndex"); 
 if (!pRoot) return;

 //access "welcom" element to get value
 TiXmlElement* pElem = pRoot->FirstChildElement("Messages")->FirstChildElement("Welcome");
 if (!pElem) return;

 //get element value (Welcome).
 char* pszNode = (char*)pElem->Value();
 if (pszNode)
  printf("Node: %s\n", pszNode);

 //get element text(Welcome to MyApp)
 char* pszText = (char*)pElem->GetText();
 if (pszText)
  printf("Text: %s\n", pszText);


}

---
The result is like that.

--------------------------
modify example.
***
#include < stdio.h>
#include "./tinyXML/tinyxml.h"


void main()
{
 TiXmlDocument doc;
 bool loadOkay = doc.LoadFile("ex4.xml");

 if (loadOkay)
 {
  printf("open success\n");
 }
 else
 {
  printf("Failed to load file \n");
 } 

 // access root node
 TiXmlElement* pRoot = doc.FirstChildElement("LibraryIndex");   
 if (!pRoot) return;  

 // access welcome element to modify value  
 TiXmlElement* pElem = pRoot->FirstChildElement("Messages")->FirstChildElement("Welcome");  
 if (!pElem) return;

 // element clear and new value adding 
 pElem->Clear();
 pElem->LinkEndChild( new TiXmlText("Edit My App"));

 doc.SaveFile("m_ex4.xml");
}
***
The result is like the figure.


-------------
Example source code to make New xml file.
In the source code, new and delete pair is do not need because created variable by new keyword will be delete automatically after block.
***
#include < stdio.h>
#include "./tinyXML/tinyxml.h"


void main()
{
 
 // Declaration XML format
 TiXmlDocument doc;
 TiXmlDeclaration* pDecl = new TiXmlDeclaration("1.0", "euc-kr", "");  
 doc.LinkEndChild(pDecl);

 //Add Root node
 TiXmlElement* pRoot = new TiXmlElement("LibraryIndex2");
 doc.LinkEndChild(pRoot);

 // Add Comment sentence
 TiXmlComment * pComment = new TiXmlComment();
 pComment->SetValue("Settings for MyApp");
 pRoot->LinkEndChild(pComment);

 // Add child node and data
 TiXmlElement* pElem = new TiXmlElement("Messages");
 pRoot->LinkEndChild(pElem);

 TiXmlElement* pSubElem = new TiXmlElement("Welcome");
 pSubElem->LinkEndChild( new TiXmlText("Welcome to MyApp"));
 pElem->LinkEndChild(pSubElem);

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

 // Add child node and set attribute 
 pElem = new TiXmlElement("Windows");
 pRoot->LinkEndChild(pElem);

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

 pElem = new TiXmlElement("Connection");
 pRoot->LinkEndChild(pElem);
 pElem->SetAttribute("ip", "192.168.0.1");
 pElem->SetDoubleAttribute("timeout", 123.456);

 //Save xml file format
 doc.SaveFile("new_ex4.xml");

}
***
The result is like the figure.

-------
Finally, This source code is searching all node and text by using recursive routine.

#include < stdio.h>
#include "./tinyXML/tinyxml.h"

void searchXMLData(TiXmlElement* pElem)
{
 TiXmlHandle hRoot(0);
 TiXmlElement* pSubElem = pElem;
 TiXmlAttribute* pAttrib = NULL;

 
 //Set current node to root node and determine childe node is exist or not
 hRoot = TiXmlHandle(pSubElem);
 pSubElem = hRoot.FirstChildElement().Element();
 if (!pSubElem) return;

 char* pszNode = NULL;
 char* pszAttrib = NULL;
 char* pszText = NULL;

 while (pSubElem)
 {
  //node
  pszNode = (char*)pSubElem->Value();
  if (pszNode)
   printf("Node: %s\n", pszNode);

  //Attribute
  pAttrib = pSubElem->FirstAttribute();
  while (pAttrib)
  {
   char* pszAttrib = (char*)pAttrib->Name();
   char* pszText = (char*)pAttrib->Value();

   printf("------------Attribute: %s, Data: %s\n", pszAttrib, pszText);

   pAttrib = pAttrib->Next();
  }
  
  //Data
  pszText = (char*)pSubElem->GetText();
  if (pszText)
   printf("Text: %s\n", pszText);

  // Recursive call for searching child node based current node
  searchXMLData(pSubElem);
  pSubElem = pSubElem->NextSiblingElement();
 }
}

int main()
{
 TiXmlDocument doc;
 doc.LoadFile("ex4.xml");
 TiXmlHandle hDoc(&doc);

 //access root node
 TiXmlElement* pRoot = hDoc.FirstChildElement().Element();
 if (!pRoot) return 0;

 
 //search childe node step by step from starting root node
 searchXMLData(pRoot);
 
 return 0;
}

The result is like the figure.


Thank you.
Good luck!.

3 comments:

  1. I try whith yout exemple but i can't parse this file.
    for exemple file shoud look like this:






    1
    2
    3
    4





    For exemple i want just data from tags , i'm not interested in all others tags. Can you help me please?

    ReplyDelete
  2. This is the first step when you find your computer overheating. Open the case, and then check if all fans are still working. If at least one is not working anymore, consider doing repairs or getting a replacement.
    find more

    ReplyDelete
  3. Thank you a bunch for sharing this with all of us you actually realize what you are talking about! Bookmarked. Please also seek advice from my site =). We could have a hyperlink change contract between us! du học Nhật Bản

    ReplyDelete