Logo ROOT   6.12/07
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 
15 void DisplayNode(TXMLEngine* xml, XMLNodePointer_t node, Int_t level);
16 
17 void xmlreadfile(const char* filename = "example.xml")
18 {
19  // First create engine
20  TXMLEngine* xml = new TXMLEngine;
21 
22  // Now try to parse xml file
23  // Only file with restricted xml syntax are supported
24  XMLDocPointer_t xmldoc = xml->ParseFile(filename);
25  if (xmldoc==0) {
26  delete xml;
27  return;
28  }
29 
30  // take access to main node
31  XMLNodePointer_t mainnode = xml->DocGetRootElement(xmldoc);
32 
33  // display recursively all nodes and subnodes
34  DisplayNode(xml, mainnode, 1);
35 
36  // Release memory before exit
37  xml->FreeDoc(xmldoc);
38  delete xml;
39 }
40 
41 void DisplayNode(TXMLEngine* xml, XMLNodePointer_t node, Int_t level)
42 {
43  // this function display all accessible information about xml node and its children
44 
45  printf("%*c node: %s\n",level,' ', xml->GetNodeName(node));
46 
47  // display namespace
48  XMLNsPointer_t ns = xml->GetNS(node);
49  if (ns!=0)
50  printf("%*c namespace: %s refer: %s\n",level+2,' ', xml->GetNSName(ns), xml->GetNSReference(ns));
51 
52  // display attributes
53  XMLAttrPointer_t attr = xml->GetFirstAttr(node);
54  while (attr!=0) {
55  printf("%*c attr: %s value: %s\n",level+2,' ', xml->GetAttrName(attr), xml->GetAttrValue(attr));
56  attr = xml->GetNextAttr(attr);
57  }
58 
59  // display content (if exists)
60  const char* content = xml->GetNodeContent(node);
61  if (content!=0)
62  printf("%*c cont: %s\n",level+2,' ', content);
63 
64  // display all child nodes
65  XMLNodePointer_t child = xml->GetChild(node);
66  while (child!=0) {
67  DisplayNode(xml, child, level+2);
68  child = xml->GetNext(child);
69  }
70 }
71 
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