Logo ROOT  
Reference Guide
xmlmodifyfile.C File Reference

Detailed Description

View in nbviewer Open in SWAN Example to read, modify and store xml file, using TXMLEngine class The input file, produced by xmlnewfile.C macro is used If you need full xml syntax support, use TXMLParser instead

#include "TXMLEngine.h"
// scan node and returns number of childs
// for each child create info node with name and number of childs
int ScanNode(TXMLEngine &xml, XMLNodePointer_t node)
{
int cnt = 0;
XMLNodePointer_t child = xml.GetChild(node);
while (child) {
cnt++;
int numsub = ScanNode(xml, child);
// create new <info> node
XMLNodePointer_t info = xml.NewChild(node, xml.GetNS(child), "info");
// set name and num attributes of info node
xml.NewAttr(info, 0, "name", xml.GetNodeName(child));
if (numsub > 0) xml.NewIntAttr(info, "num", numsub);
// move it after current node
xml.AddChildAfter(node, info, child);
// set pointer to new node
child = info;
xml.ShiftToNext(child);
}
return cnt;
}
void xmlmodifyfile(const char* filename = "example.xml")
{
// First create engine
// Now try to parse xml file
XMLDocPointer_t xmldoc = xml.ParseFile(filename);
if (xmldoc) {
// recursively scan all nodes, insert new when required
ScanNode(xml, xml.DocGetRootElement(xmldoc));
// Save document to file
xml.SaveDoc(xmldoc, "modify.xml");
// Release memory before exit
xml.FreeDoc(xmldoc);
}
}
Author
Sergey Linev

Definition in file xmlmodifyfile.C.

TXMLEngine::NewAttr
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:583
TXMLEngine::NewChild
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:712
XMLDocPointer_t
void * XMLDocPointer_t
Definition: TXMLEngine.h:20
TXMLEngine::SaveDoc
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...
Definition: TXMLEngine.cxx:1303
TXMLEngine::ShiftToNext
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
Definition: TXMLEngine.cxx:1179
TXMLEngine::DocGetRootElement
XMLNodePointer_t DocGetRootElement(XMLDocPointer_t xmldoc)
returns root node of document
Definition: TXMLEngine.cxx:1336
TXMLEngine::ParseFile
XMLDocPointer_t ParseFile(const char *filename, Int_t maxbuf=100000)
Parses content of file and tries to produce xml structures.
Definition: TXMLEngine.cxx:1353
TXMLEngine::GetChild
XMLNodePointer_t GetChild(XMLNodePointer_t xmlnode, Bool_t realnode=kTRUE)
returns first child of xmlnode
Definition: TXMLEngine.cxx:1143
TXMLEngine::AddChildAfter
void AddChildAfter(XMLNodePointer_t parent, XMLNodePointer_t child, XMLNodePointer_t afternode)
Insert new child node after already existing node.
Definition: TXMLEngine.cxx:843
TXMLEngine.h
TXMLEngine
Definition: TXMLEngine.h:26
TXMLEngine::GetNS
XMLNsPointer_t GetNS(XMLNodePointer_t xmlnode)
return namespace attribute (if exists)
Definition: TXMLEngine.cxx:763
XMLNodePointer_t
void * XMLNodePointer_t
Definition: TXMLEngine.h:17
xmlio::cnt
const char * cnt
Definition: TXMLSetup.cxx:81
TXMLEngine::GetNodeName
const char * GetNodeName(XMLNodePointer_t xmlnode)
returns name of xmlnode
Definition: TXMLEngine.cxx:1072
TXMLEngine::FreeDoc
void FreeDoc(XMLDocPointer_t xmldoc)
frees allocated document data and deletes document itself
Definition: TXMLEngine.cxx:1285
TXMLEngine::NewIntAttr
XMLAttrPointer_t NewIntAttr(XMLNodePointer_t xmlnode, const char *name, Int_t value)
create node attribute with integer value
Definition: TXMLEngine.cxx:609