ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
xmlreadfile.C
Go to the documentation of this file.
1 // Example to read and parse any xml file, supported by TXMLEngine class
2 // The input file, produced by xmlnewfile.C macro is used
3 // If you need full xml syntax support, use TXMLParser instead
4 //Author: Sergey Linev
5 
6 #include "TXMLEngine.h"
7 
8 void DisplayNode(TXMLEngine* xml, XMLNodePointer_t node, Int_t level);
9 
10 void xmlreadfile(const char* filename = "example.xml")
11 {
12  // First create engine
13  TXMLEngine* xml = new TXMLEngine;
14 
15  // Now try to parse xml file
16  // Only file with restricted xml syntax are supported
17  XMLDocPointer_t xmldoc = xml->ParseFile(filename);
18  if (xmldoc==0) {
19  delete xml;
20  return;
21  }
22 
23  // take access to main node
24  XMLNodePointer_t mainnode = xml->DocGetRootElement(xmldoc);
25 
26  // display recursively all nodes and subnodes
27  DisplayNode(xml, mainnode, 1);
28 
29  // Release memory before exit
30  xml->FreeDoc(xmldoc);
31  delete xml;
32 }
33 
35 {
36  // this function display all accessible information about xml node and its children
37 
38  printf("%*c node: %s\n",level,' ', xml->GetNodeName(node));
39 
40  // display namespace
41  XMLNsPointer_t ns = xml->GetNS(node);
42  if (ns!=0)
43  printf("%*c namespace: %s refer: %s\n",level+2,' ', xml->GetNSName(ns), xml->GetNSReference(ns));
44 
45  // display attributes
46  XMLAttrPointer_t attr = xml->GetFirstAttr(node);
47  while (attr!=0) {
48  printf("%*c attr: %s value: %s\n",level+2,' ', xml->GetAttrName(attr), xml->GetAttrValue(attr));
49  attr = xml->GetNextAttr(attr);
50  }
51 
52  // display content (if exists)
53  const char* content = xml->GetNodeContent(node);
54  if (content!=0)
55  printf("%*c cont: %s\n",level+2,' ', content);
56 
57  // display all child nodes
58  XMLNodePointer_t child = xml->GetChild(node);
59  while (child!=0) {
60  DisplayNode(xml, child, level+2);
61  child = xml->GetNext(child);
62  }
63 }
64 
XMLNsPointer_t GetNS(XMLNodePointer_t xmlnode)
return namespace attribute (if exists)
Definition: TXMLEngine.cxx:665
static const char * filename()
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:582
void FreeDoc(XMLDocPointer_t xmldoc)
frees allocated document data and deletes document itself
const char * GetNodeContent(XMLNodePointer_t xmlnode)
get contents (if any) of xml node
Definition: TXMLEngine.cxx:938
void * XMLDocPointer_t
Definition: TXMLEngine.h:22
const char * GetNodeName(XMLNodePointer_t xmlnode)
returns name of xmlnode
Definition: TXMLEngine.cxx:930
const char * GetAttrValue(XMLAttrPointer_t xmlattr)
return value of attribute
Definition: TXMLEngine.cxx:603
XMLAttrPointer_t GetFirstAttr(XMLNodePointer_t xmlnode)
return first attribute in the list, namespace (if exists) will be skiped
Definition: TXMLEngine.cxx:568
void * XMLNodePointer_t
Definition: TXMLEngine.h:19
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:21
ClassImp(TMCParticle) void TMCParticle printf(": p=(%7.3f,%7.3f,%9.3f) ;", fPx, fPy, fPz)
void * XMLNsPointer_t
Definition: TXMLEngine.h:20
XMLNodePointer_t GetChild(XMLNodePointer_t xmlnode, Bool_t realnode=kTRUE)
returns first child of xml node
Definition: TXMLEngine.cxx:993
const char * GetNSName(XMLNsPointer_t ns)
return name id of namespace
Definition: TXMLEngine.cxx:676
void xmlreadfile(const char *filename="example.xml")
Definition: xmlreadfile.C:10
XMLNodePointer_t DocGetRootElement(XMLDocPointer_t xmldoc)
returns root node of document
void DisplayNode(TXMLEngine *xml, XMLNodePointer_t node, Int_t level)
Definition: xmlreadfile.C:34
const char * GetAttrName(XMLAttrPointer_t xmlattr)
return name of the attribute
Definition: TXMLEngine.cxx:592
const char * GetNSReference(XMLNsPointer_t ns)
return reference id of namespace
Definition: TXMLEngine.cxx:688