File indexing completed on 2025-01-18 10:18:22
0001
0002 #include <vector>
0003
0004 #include <xercesc/dom/DOM.hpp>
0005 #include <xercesc/parsers/XercesDOMParser.hpp>
0006
0007 #ifndef _XML_TREE_
0008 #define _XML_TREE_
0009
0010 class XmlTree {
0011 public:
0012 XmlTree(xercesc::DOMElement *element): m_Element(element) {
0013 m_TagName = element->getTagName();
0014
0015 xercesc::DOMNodeList *children = element->getChildNodes();
0016 const XMLSize_t nodeCount = children->getLength();
0017
0018 for( XMLSize_t xx = 0; xx < nodeCount; ++xx ) {
0019 xercesc::DOMNode* currentNode = children->item(xx);
0020 auto type = currentNode->getNodeType();
0021
0022
0023 if (type == xercesc::DOMNode::ELEMENT_NODE) {
0024 auto branch = new XmlTree(dynamic_cast< xercesc::DOMElement* >( currentNode ));
0025 m_Branches.push_back(branch);
0026 }
0027 else if (type == xercesc::DOMNode::TEXT_NODE)
0028 m_Strings.push_back(dynamic_cast< xercesc::DOMText* >( currentNode ));
0029 }
0030 };
0031 ~XmlTree() {};
0032
0033 void Print(unsigned indent = 0) const {
0034 for(unsigned iq=0; iq<indent; iq++)
0035 printf(" ");
0036 printf("%s (%2d)\n", xercesc::XMLString::transcode(m_TagName), (int)m_Strings.size());
0037 for(unsigned iq=0; iq<indent; iq++)
0038 printf(" ");
0039 for(auto str: m_Strings)
0040 printf(" %s\n", xercesc::XMLString::transcode(str->getWholeText()));
0041
0042 for(auto branch: m_Branches)
0043 branch->Print(indent + 6);
0044 };
0045
0046 xercesc::DOMElement *GetElement( void ) const { return m_Element; };
0047 const char *GetName( void ) const { return xercesc::XMLString::transcode(m_TagName); };
0048 const XMLCh *GetAttribute(const char *attr) {
0049 return m_Element->getAttribute(xercesc::XMLString::transcode(attr));
0050 };
0051
0052 std::vector<XmlTree*> FindTags(const char *tag) const {
0053 std::vector<XmlTree*> ret;
0054
0055 for(auto branch: m_Branches)
0056 if (xercesc::XMLString::equals(tag, branch->GetName()))
0057 ret.push_back(branch);
0058
0059 return ret;
0060 };
0061 XmlTree *FindFirstTag(const char *tag) const {
0062 for(auto branch: m_Branches)
0063 if (xercesc::XMLString::equals(tag, branch->GetName()))
0064 return branch;
0065
0066 return 0;
0067 };
0068 const char *GetFirstString( void ) const {
0069 return (m_Strings.size() ? xercesc::XMLString::transcode(m_Strings[0]->getWholeText()) : 0);
0070 };
0071
0072 private:
0073 xercesc::DOMElement *m_Element;
0074 const XMLCh *m_TagName;
0075 std::vector<xercesc::DOMText*> m_Strings;
0076 std::vector<XmlTree*> m_Branches;
0077 };
0078
0079 #endif