Logo ROOT  
Reference Guide
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_xml
3/// \notebook -nodraw
4/// Example to read, modify and store xml file, using TXMLEngine class
5/// The input file, produced by xmlnewfile.C macro is used
6/// If you need full xml syntax support, use TXMLParser instead
8/// \macro_output
9/// \macro_code
11/// \author Sergey Linev
13#include "TXMLEngine.h"
15// scan node and returns number of childs
16// for each child create info node with name and number of childs
17int ScanNode(TXMLEngine &xml, XMLNodePointer_t node)
19 int cnt = 0;
20 XMLNodePointer_t child = xml.GetChild(node);
21 while (child) {
22 cnt++;
24 int numsub = ScanNode(xml, child);
26 // create new <info> node
27 XMLNodePointer_t info = xml.NewChild(node, xml.GetNS(child), "info");
29 // set name and num attributes of info node
30 xml.NewAttr(info, 0, "name", xml.GetNodeName(child));
31 if (numsub > 0) xml.NewIntAttr(info, "num", numsub);
33 // move it after current node
34 xml.AddChildAfter(node, info, child);
36 // set pointer to new node
37 child = info;
39 xml.ShiftToNext(child);
40 }
41 return cnt;
44void xmlmodifyfile(const char* filename = "example.xml")
46 // First create engine
47 TXMLEngine xml;
49 // Now try to parse xml file
50 XMLDocPointer_t xmldoc = xml.ParseFile(filename);
51 if (xmldoc) {
52 // recursively scan all nodes, insert new when required
53 ScanNode(xml, xml.DocGetRootElement(xmldoc));
55 // Save document to file
56 xml.SaveDoc(xmldoc, "modify.xml");
58 // Release memory before exit
59 xml.FreeDoc(xmldoc);
60 }
void * XMLNodePointer_t
Definition: TXMLEngine.h:17
void * XMLDocPointer_t
Definition: TXMLEngine.h:20
XMLNodePointer_t NewChild(XMLNodePointer_t parent, XMLNsPointer_t ns, const char *name, const char *content=nullptr)
create new child element for parent node
Definition: TXMLEngine.cxx:715
XMLNodePointer_t GetChild(XMLNodePointer_t xmlnode, Bool_t realnode=kTRUE)
returns first child of xmlnode
XMLAttrPointer_t NewAttr(XMLNodePointer_t xmlnode, XMLNsPointer_t, const char *name, const char *value)
creates new attribute for xmlnode, namespaces are not supported for attributes
Definition: TXMLEngine.cxx:586
void SaveDoc(XMLDocPointer_t xmldoc, const char *filename, Int_t layout=1)
store document content to file if layout<=0, no any spaces or newlines will be placed between xmlnode...
void FreeDoc(XMLDocPointer_t xmldoc)
frees allocated document data and deletes document itself
XMLNodePointer_t DocGetRootElement(XMLDocPointer_t xmldoc)
returns root node of document
XMLAttrPointer_t NewIntAttr(XMLNodePointer_t xmlnode, const char *name, Int_t value)
create node attribute with integer value
Definition: TXMLEngine.cxx:612
XMLNsPointer_t GetNS(XMLNodePointer_t xmlnode)
return namespace attribute (if exists)
Definition: TXMLEngine.cxx:766
const char * GetNodeName(XMLNodePointer_t xmlnode)
returns name of xmlnode
void AddChildAfter(XMLNodePointer_t parent, XMLNodePointer_t child, XMLNodePointer_t afternode)
Insert new child node after already existing node.
Definition: TXMLEngine.cxx:846
XMLDocPointer_t ParseFile(const char *filename, Int_t maxbuf=100000)
Parses content of file and tries to produce xml structures.
void ShiftToNext(XMLNodePointer_t &xmlnode, Bool_t realnode=kTRUE)
shifts specified node to next if realnode==kTRUE, any special nodes in between will be skipped
const char * cnt
Definition: TXMLSetup.cxx:75