Xmlreadfile

Example to read and parse any xml file, supported by 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 Saturday, September 18, 2021 at 10:02 AM.

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

A helper function is created:

In [2]:
%%cpp -d
void DisplayNode(TXMLEngine &xml, XMLNodePointer_t node, Int_t level)
{
   // this function display all accessible information about xml node and its children

   printf("%*c node: %s\n", level, ' ', xml.GetNodeName(node));

   // display namespace
   XMLNsPointer_t ns = xml.GetNS(node);
   if (ns != 0)
      printf("%*c namespace: %s refer: %s\n", level + 2, ' ', xml.GetNSName(ns), xml.GetNSReference(ns));

   // display attributes
   XMLAttrPointer_t attr = xml.GetFirstAttr(node);
   while (attr != 0) {
      printf("%*c attr: %s value: %s\n", level + 2, ' ', xml.GetAttrName(attr), xml.GetAttrValue(attr));
      attr = xml.GetNextAttr(attr);
   }

   // display content (if exists)
   const char *content = xml.GetNodeContent(node);
   if (content != 0)
      printf("%*c cont: %s\n", level + 2, ' ', content);

   // display all child nodes
   XMLNodePointer_t child = xml.GetChild(node);
   while (child != 0) {
      DisplayNode(xml, child, level + 2);
      child = xml.GetNext(child);
   }
}

Arguments are defined.

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

First create engine

In [4]:
TXMLEngine xml;

Now try to parse xml file Only file with restricted xml syntax are supported

In [5]:
XMLDocPointer_t xmldoc = xml.ParseFile(filename);
if (!xmldoc) return;

Take access to main node

In [6]:
XMLNodePointer_t mainnode = xml.DocGetRootElement(xmldoc);

Display recursively all nodes and subnodes

In [7]:
DisplayNode(xml, mainnode, 1);
  node: main
    node: child1
      cont: Content of child1 node
    node: child2
      attr: attr1 value: value1
      attr: attr2 value: value2
    node: child3
      node: subchild1
        cont: subchild1 content
      node: subchild2
        cont: subchild2 content
      node: subchild3
        cont: subchild3 content
    node: child4
      namespace: child4 refer: http://website/webpage
      node: subchild1
        namespace: child4 refer: http://website/webpage
        cont: subchild1 content
      node: subchild2
        namespace: child4 refer: http://website/webpage
        cont: subchild2 content
      node: subchild3
        namespace: child4 refer: http://website/webpage
        cont: subchild3 content

Release memory before exit

In [8]:
xml.FreeDoc(xmldoc);