Xmlmodifyfile

Example to read, modify and store xml file, using TXMLEngine class The input file, produced by xmlnewfile.C macro is used If you need full xml syntax support, use TXMLParser instead

Author: Sergey Linev
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Monday, September 20, 2021 at 03:15 PM.

In [1]:
%%cpp -d
#include "TXMLEngine.h"

scan node and returns number of childs for each child create info node with name and number of childs

In [2]:
%%cpp -d
int ScanNode(TXMLEngine &xml, XMLNodePointer_t node)
{
   int cnt = 0;
   XMLNodePointer_t child = xml.GetChild(node);
   while (child) {
      cnt++;

      int numsub = ScanNode(xml, child);

      // create new <info> node
      XMLNodePointer_t info = xml.NewChild(node, xml.GetNS(child), "info");

      // set name and num attributes of info node
      xml.NewAttr(info, 0, "name", xml.GetNodeName(child));
      if (numsub > 0) xml.NewIntAttr(info, "num", numsub);

      // move it after current node
      xml.AddChildAfter(node, info, child);

      // set pointer to new node
      child = info;

      xml.ShiftToNext(child);
   }
   return cnt;
}

Arguments are defined.

In [3]:
const char* filename = "example.xml";

First create engine

In [4]:
TXMLEngine xml;

Now try to parse xml file

In [5]:
XMLDocPointer_t xmldoc = xml.ParseFile(filename);
if (xmldoc) {
   // recursively scan all nodes, insert new when required
   ScanNode(xml, xml.DocGetRootElement(xmldoc));

   // Save document to file
   xml.SaveDoc(xmldoc, "modify.xml");

   // Release memory before exit
   xml.FreeDoc(xmldoc);
}
Error in <TXMLEngine::ParseFile>: Unexpected end of xml file