Logo ROOT   6.14/05
Reference Guide
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 }
XMLNsPointer_t GetNS(XMLNodePointer_t xmlnode)
return namespace attribute (if exists)
Definition: TXMLEngine.cxx:758
XMLNodePointer_t GetNext(XMLNodePointer_t xmlnode, Bool_t realnode=kTRUE)
return next to xmlnode node if realnode==kTRUE, any special nodes in between will be skipped ...
int Int_t
Definition: RtypesCore.h:41
XMLAttrPointer_t GetNextAttr(XMLAttrPointer_t xmlattr)
return next attribute in the list
Definition: TXMLEngine.cxx:673
void FreeDoc(XMLDocPointer_t xmldoc)
frees allocated document data and deletes document itself
const char * GetNodeContent(XMLNodePointer_t xmlnode)
get contents (if any) of xmlnode
void * XMLDocPointer_t
Definition: TXMLEngine.h:20
const char * GetNodeName(XMLNodePointer_t xmlnode)
returns name of xmlnode
const char * GetAttrValue(XMLAttrPointer_t xmlattr)
return value of attribute
Definition: TXMLEngine.cxx:695
XMLAttrPointer_t GetFirstAttr(XMLNodePointer_t xmlnode)
return first attribute in the list, namespace (if exists) will be skipped
Definition: TXMLEngine.cxx:657
void * XMLNodePointer_t
Definition: TXMLEngine.h:17
XMLDocPointer_t ParseFile(const char *filename, Int_t maxbuf=100000)
Parses content of file and tries to produce xml structures.
void * XMLAttrPointer_t
Definition: TXMLEngine.h:19
void * XMLNsPointer_t
Definition: TXMLEngine.h:18
XMLNodePointer_t GetChild(XMLNodePointer_t xmlnode, Bool_t realnode=kTRUE)
returns first child of xmlnode
const char * GetNSName(XMLNsPointer_t ns)
return name id of namespace
Definition: TXMLEngine.cxx:770
XMLNodePointer_t DocGetRootElement(XMLDocPointer_t xmldoc)
returns root node of document
const char * GetAttrName(XMLAttrPointer_t xmlattr)
return name of the attribute
Definition: TXMLEngine.cxx:684
static constexpr double ns
const char * GetNSReference(XMLNsPointer_t ns)
return reference id of namespace
Definition: TXMLEngine.cxx:783