00001 ////////////////////////////////////////////////////////////////////////////// 00002 // 00003 // ROOT implementation of a XML DOM Parser 00004 // 00005 // This is an example of how Dom Parser walks the DOM tree recursively. 00006 // This example will parse any xml file. 00007 // 00008 // To run this program 00009 // .x DOMRecursive.C+ 00010 // 00011 // Requires: person.xml 00012 // 00013 ////////////////////////////////////////////////////////////////////////////// 00014 00015 #include <Riostream.h> 00016 #include <TDOMParser.h> 00017 #include <TXMLNode.h> 00018 #include <TXMLAttr.h> 00019 #include <TList.h> 00020 00021 00022 void ParseContext(TXMLNode *node) 00023 { 00024 for ( ; node; node = node->GetNextNode()) { 00025 if (node->GetNodeType() == TXMLNode::kXMLElementNode) { // Element Node 00026 cout << node->GetNodeName() << ": "; 00027 if (node->HasAttributes()) { 00028 TList* attrList = node->GetAttributes(); 00029 TIter next(attrList); 00030 TXMLAttr *attr; 00031 while ((attr =(TXMLAttr*)next())) { 00032 cout << attr->GetName() << ":" << attr->GetValue(); 00033 } 00034 } 00035 } 00036 if (node->GetNodeType() == TXMLNode::kXMLTextNode) { // Text node 00037 cout << node->GetContent(); 00038 } 00039 if (node->GetNodeType() == TXMLNode::kXMLCommentNode) { //Comment node 00040 cout << "Comment: " << node->GetContent(); 00041 } 00042 00043 ParseContext(node->GetChildren()); 00044 } 00045 } 00046 00047 00048 void DOMRecursive() 00049 { 00050 TDOMParser *domParser = new TDOMParser(); 00051 00052 domParser->SetValidate(false); // do not validate with DTD 00053 domParser->ParseFile("person.xml"); 00054 00055 TXMLNode *node = domParser->GetXMLDocument()->GetRootNode(); 00056 00057 ParseContext(node); 00058 }