Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
xmlreadfile.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_xml
3 /// \notebook -nodraw
4 /// Example to read and parse any xml file, supported by TXMLEngine class
5 /// The input file, produced by xmlnewfile.C macro is used
6 /// If you need full xml syntax support, use TXMLParser instead
7 ///
8 /// \macro_output
9 /// \macro_code
10 ///
11 /// \author Sergey Linev
12 
13 #include "TXMLEngine.h"
14 #include <stdio.h>
15 
16 void DisplayNode(TXMLEngine &xml, XMLNodePointer_t node, Int_t level)
17 {
18  // this function display all accessible information about xml node and its children
19 
20  printf("%*c node: %s\n", level, ' ', xml.GetNodeName(node));
21 
22  // display namespace
23  XMLNsPointer_t ns = xml.GetNS(node);
24  if (ns != 0)
25  printf("%*c namespace: %s refer: %s\n", level + 2, ' ', xml.GetNSName(ns), xml.GetNSReference(ns));
26 
27  // display attributes
28  XMLAttrPointer_t attr = xml.GetFirstAttr(node);
29  while (attr != 0) {
30  printf("%*c attr: %s value: %s\n", level + 2, ' ', xml.GetAttrName(attr), xml.GetAttrValue(attr));
31  attr = xml.GetNextAttr(attr);
32  }
33 
34  // display content (if exists)
35  const char *content = xml.GetNodeContent(node);
36  if (content != 0)
37  printf("%*c cont: %s\n", level + 2, ' ', content);
38 
39  // display all child nodes
40  XMLNodePointer_t child = xml.GetChild(node);
41  while (child != 0) {
42  DisplayNode(xml, child, level + 2);
43  child = xml.GetNext(child);
44  }
45 }
46 
47 void xmlreadfile(const char* filename = "example.xml")
48 {
49  // First create engine
50  TXMLEngine xml;
51 
52  // Now try to parse xml file
53  // Only file with restricted xml syntax are supported
54  XMLDocPointer_t xmldoc = xml.ParseFile(filename);
55  if (!xmldoc) return;
56 
57  // take access to main node
58  XMLNodePointer_t mainnode = xml.DocGetRootElement(xmldoc);
59 
60  // display recursively all nodes and subnodes
61  DisplayNode(xml, mainnode, 1);
62 
63  // Release memory before exit
64  xml.FreeDoc(xmldoc);
65 }