xmlreadfile.C

Go to the documentation of this file.
00001 // Example to read and parse any xml file, supported by TXMLEngine class
00002 // The input file, produced by xmlnewfile.C macro is used
00003 // If you need full xml syntax support, use TXMLParser instead
00004 //Author: Sergey Linev
00005    
00006 #include "TXMLEngine.h"
00007 
00008 void DisplayNode(TXMLEngine* xml, XMLNodePointer_t node, Int_t level);
00009 
00010 void xmlreadfile(const char* filename = "example.xml")
00011 {
00012    // First create engine
00013    TXMLEngine* xml = new TXMLEngine;
00014    
00015    // Now try to parse xml file
00016    // Only file with restricted xml syntax are supported
00017    XMLDocPointer_t xmldoc = xml->ParseFile(filename);
00018    if (xmldoc==0) {
00019       delete xml;
00020       return;  
00021    }
00022 
00023    // take access to main node   
00024    XMLNodePointer_t mainnode = xml->DocGetRootElement(xmldoc);
00025    
00026    // display recursively all nodes and subnodes
00027    DisplayNode(xml, mainnode, 1);
00028    
00029    // Release memory before exit
00030    xml->FreeDoc(xmldoc);
00031    delete xml;
00032 }
00033 
00034 void DisplayNode(TXMLEngine* xml, XMLNodePointer_t node, Int_t level) 
00035 {
00036    // this function display all accessible information about xml node and its childs
00037    
00038    printf("%*c node: %s\n",level,' ', xml->GetNodeName(node));
00039    
00040    // display namespace
00041    XMLNsPointer_t ns = xml->GetNS(node);
00042    if (ns!=0)
00043       printf("%*c namespace: %s refer: %s\n",level+2,' ', xml->GetNSName(ns), xml->GetNSReference(ns));
00044    
00045    // display attributes
00046    XMLAttrPointer_t attr = xml->GetFirstAttr(node);
00047    while (attr!=0) {
00048        printf("%*c attr: %s value: %s\n",level+2,' ', xml->GetAttrName(attr), xml->GetAttrValue(attr));
00049        attr = xml->GetNextAttr(attr);  
00050    }
00051    
00052    // display content (if exists)
00053    const char* content = xml->GetNodeContent(node);
00054    if (content!=0) 
00055       printf("%*c cont: %s\n",level+2,' ', content);
00056       
00057    // display all child nodes   
00058    XMLNodePointer_t child = xml->GetChild(node);
00059    while (child!=0) {
00060       DisplayNode(xml, child, level+2); 
00061       child = xml->GetNext(child);
00062    }
00063 }
00064 

Generated on Tue Jul 5 15:45:12 2011 for ROOT_528-00b_version by  doxygen 1.5.1