Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
xmlmodifyfile.C
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
7 ///
8 /// \macro_output
9 /// \macro_code
10 ///
11 /// \author Sergey Linev
12 
13 #include "TXMLEngine.h"
14 
15 // scan node and returns number of childs
16 // for each child create info node with name and number of childs
17 int ScanNode(TXMLEngine &xml, XMLNodePointer_t node)
18 {
19  int cnt = 0;
20  XMLNodePointer_t child = xml.GetChild(node);
21  while (child) {
22  cnt++;
23 
24  int numsub = ScanNode(xml, child);
25 
26  // create new <info> node
27  XMLNodePointer_t info = xml.NewChild(node, xml.GetNS(child), "info");
28 
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);
32 
33  // move it after current node
34  xml.AddChildAfter(node, info, child);
35 
36  // set pointer to new node
37  child = info;
38 
39  xml.ShiftToNext(child);
40  }
41  return cnt;
42 }
43 
44 void xmlmodifyfile(const char* filename = "example.xml")
45 {
46  // First create engine
47  TXMLEngine xml;
48 
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));
54 
55  // Save document to file
56  xml.SaveDoc(xmldoc, "modify.xml");
57 
58  // Release memory before exit
59  xml.FreeDoc(xmldoc);
60  }
61 }