使用 ACEXML 来解析一个 xml 文件

前端之家收集整理的这篇文章主要介绍了使用 ACEXML 来解析一个 xml 文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

本段代码通过ACEXML来解析一个XML文件,并且生成了一棵树,树的存储采用孩子兄弟存储的方法

在连接生成可执行文件的时候,连接一下文件


ACEd.lib
ACEXMLd.lib
ACEXML_Parserd.lib
iphlpapi.lib


CTree


  1. #pragma once
  2.  
  3. #include "string"
  4.  
  5. using namespace std;
  6.  
  7. class CTree
  8. {
  9. public:
  10. typedef enum{LEAF = 0x00,NOTLEAF = 0x01} ATTRIBUTE;
  11. public:
  12. CTree(void);
  13. CTree(ATTRIBUTE attribute,string value);
  14. ~CTree(void);
  15. private:
  16. ATTRIBUTE m_attribute;
  17. string m_value;
  18. CTree* m_left;
  19. CTree* m_right;
  20. public:
  21. void addnode(CTree* children);
  22. void output(int indent);
  23. ATTRIBUTE getattribute();
  24. string getvalue();
  25. CTree* getleft();
  26. CTree* getright();
  27. int getchildnumber();
  28. CTree* getfirstchild();
  29. CTree* getnextchild();
  30. CTree* getchild(int index);
  31. void destroy();
  32. };
  33.  
  34.  
  35. #include "Tree.h"
  36.  
  37.  
  38. CTree::CTree(void)
  39. {
  40. m_attribute = NOTLEAF;
  41. m_value = "";
  42. m_left = NULL;
  43. m_right = NULL;
  44. }
  45.  
  46. CTree::CTree(ATTRIBUTE attribute,string value)
  47. {
  48. m_attribute = attribute;
  49. m_value = value;
  50. m_left = NULL;
  51. m_right = NULL;
  52. }
  53.  
  54.  
  55. CTree::~CTree(void)
  56. {
  57. printf("destroy value=%s\n",m_value.c_str());
  58. }
  59.  
  60.  
  61. CTree::ATTRIBUTE CTree::getattribute()
  62. {
  63. return m_attribute;
  64. }
  65.  
  66. CTree* CTree::getleft()
  67. {
  68. return m_left;
  69. }
  70.  
  71. CTree* CTree::getright()
  72. {
  73. return m_right;
  74. }
  75.  
  76. string CTree::getvalue()
  77. {
  78. return m_value;
  79. }
  80.  
  81. void CTree::destroy()
  82. {
  83. if (m_left != NULL)
  84. {
  85. m_left->destroy();
  86. }
  87. if (m_right != NULL)
  88. {
  89. m_right->destroy();
  90. }
  91. delete this;
  92. }
  93.  
  94. void CTree::addnode(CTree* child)
  95. {
  96. if (m_left == NULL)
  97. {
  98. m_left = child;
  99. }
  100. else
  101. {
  102. CTree* ptemp = m_left;
  103. while(ptemp->m_right != NULL)
  104. {
  105. ptemp = ptemp->m_right;
  106. }
  107. ptemp->m_right = child;
  108. }
  109. }
  110.  
  111. void CTree::output(int indent)
  112. {
  113. //printf("value=%s attri=%c this=%08X left=%08X right=%08X\n",m_value.c_str(),((m_attribute == LEAF) ? 'C' : 'P'),this,m_left,m_right);
  114. //printf("%s(deep=%d)\n",indent);
  115. printf("%s\n",m_value.c_str());
  116.  
  117. if (m_left != NULL)
  118. {
  119. indent++;
  120. for(int i=0; i<indent; i++)
  121. {
  122. printf("|____");
  123. }
  124. m_left->output(indent);
  125. indent--;
  126. }
  127.  
  128. if (m_right != NULL)
  129. {
  130. for(int i = 0; i < indent; i++)
  131. {
  132. printf("|____");
  133. }
  134. m_right->output(indent);
  135. }
  136. }
  137.  
  138.  
  139. int CTree::getchildnumber()
  140. {
  141. int number = 0;
  142. if (m_left == NULL)
  143. {
  144. return number;
  145. }
  146. else
  147. {
  148. number++;
  149. CTree* temp = m_left;
  150. while(temp->m_right != NULL)
  151. {
  152. number++;
  153. temp = temp->m_right;
  154. }
  155. }
  156. return number;
  157. }
  158.  
  159. CTree* CTree::getfirstchild()
  160. {
  161. return m_left;
  162. }
  163.  
  164.  
  165. CTree* CTree::getnextchild()
  166. {
  167. return m_right;
  168. }
  169.  
  170. CTree* CTree::getchild(int index)
  171. {
  172. CTree* temp = NULL;
  173. bool find = false;
  174.  
  175. int count = 1;
  176. if (index == 1)
  177. {
  178. find = true;
  179. temp = m_left;
  180. }
  181. else
  182. {
  183. temp = m_left;
  184. while(temp->m_right != NULL)
  185. {
  186. count++;
  187. if (count == index)
  188. {
  189. find = true;
  190. break;
  191. }
  192. temp = temp->m_right;
  193. }
  194. }
  195.  
  196. if (find)
  197. {
  198. return temp;
  199. }
  200. else
  201. {
  202. temp = NULL;
  203. return temp;
  204. }
  205. }


CStack



  1. #pragma once
  2.  
  3. #include "Stack"
  4. class CTree;
  5. using namespace std;
  6.  
  7. typedef stack<CTree*> STACK;
  8.  
  9. class CStack
  10. {
  11. public:
  12. CStack(void);
  13. ~CStack(void);
  14. private:
  15. STACK m_stack;
  16. public:
  17. void push(CTree* node);
  18. CTree* pop();
  19. CTree* get();
  20. int size();
  21. bool empty();
  22. };
  23.  
  24.  
  25. #include "Stack.h"
  26.  
  27.  
  28. CStack::CStack(void)
  29. {
  30. }
  31.  
  32.  
  33. CStack::~CStack(void)
  34. {
  35. }
  36.  
  37. void CStack:: push(CTree* node)
  38. {
  39. m_stack.push(node);
  40. }
  41.  
  42. CTree* CStack::pop()
  43. {
  44. CTree* node = NULL;
  45. if (!empty())
  46. {
  47. node = get();
  48. m_stack.pop();
  49. }
  50. return node;
  51. }
  52.  
  53. CTree* CStack::get()
  54. {
  55. CTree* node = NULL;
  56. if (!empty())
  57. {
  58. node = m_stack.top();
  59. }
  60. return node;
  61. }
  62.  
  63.  
  64. int CStack::size()
  65. {
  66. return m_stack.size();
  67. }
  68.  
  69. bool CStack::empty()
  70. {
  71. return m_stack.empty();
  72. }
  73.  
  74.  



CXMLParse



  1. #pragma once
  2.  
  3. #include "ACEXML/common/DefaultHandler.h"
  4. #include "string"
  5. #include "Tree.h"
  6. #include "Stack.h"
  7.  
  8. using namespace std;
  9.  
  10.  
  11. class CXMLParse : public ACEXML_DefaultHandler
  12. {
  13. public:
  14. CXMLParse(ACEXML_Char* fileName);
  15. virtual ~CXMLParse(void);
  16. public:
  17. // Methods inherited from ACEXML_ContentHandler.
  18.  
  19. /*
  20. * Receive notification of character data.
  21. */
  22. virtual void characters (const ACEXML_Char *ch,size_t start,size_t length);
  23.  
  24. /*
  25. * Receive notification of the end of a document.
  26. */
  27. virtual void endDocument (void);
  28.  
  29. /*
  30. * Receive notification of the end of an element.
  31. */
  32. virtual void endElement (const ACEXML_Char *namespaceURI,const ACEXML_Char *localName,const ACEXML_Char *qName);
  33.  
  34. /*
  35. * End the scope of a prefix-URI mapping.
  36. */
  37. virtual void endPrefixMapping (const ACEXML_Char *prefix);
  38.  
  39. /*
  40. * Receive notification of ignorable whitespace in element content.
  41. */
  42. virtual void ignorableWhitespace (const ACEXML_Char *ch,int start,int length);
  43.  
  44. /*
  45. * Receive notification of a processing instruction.
  46. */
  47. virtual void processingInstruction (const ACEXML_Char *target,const ACEXML_Char *data);
  48.  
  49. /*
  50. * Receive an object for locating the origin of SAX document events.
  51. */
  52. virtual void setDocumentLocator (ACEXML_Locator *locator) ;
  53.  
  54. /*
  55. * Receive notification of a skipped entity.
  56. */
  57. virtual void skippedEntity (const ACEXML_Char *name);
  58.  
  59. /*
  60. * Receive notification of the beginning of a document.
  61. */
  62. virtual void startDocument (void);
  63.  
  64. /*
  65. * Receive notification of the beginning of an element.
  66. */
  67. virtual void startElement (const ACEXML_Char *namespaceURI,const ACEXML_Char *qName,ACEXML_Attributes *atts);
  68.  
  69. /*
  70. * Begin the scope of a prefix-URI Namespace mapping.
  71. */
  72. virtual void startPrefixMapping (const ACEXML_Char *prefix,const ACEXML_Char *uri);
  73.  
  74. // *** Methods inherit from ACEXML_DTDHandler.
  75.  
  76. /*
  77. * Receive notification of a notation declaration event.
  78. */
  79. virtual void notationDecl (const ACEXML_Char *name,const ACEXML_Char *publicId,const ACEXML_Char *systemId);
  80.  
  81. /*
  82. * Receive notification of an unparsed entity declaration event.
  83. */
  84. virtual void unparsedEntityDecl (const ACEXML_Char *name,const ACEXML_Char *systemId,const ACEXML_Char *notationName);
  85.  
  86. // Methods inherit from ACEXML_EnitityResolver.
  87.  
  88. /*
  89. * Allow the application to resolve external entities.
  90. */
  91. virtual ACEXML_InputSource *resolveEntity (const ACEXML_Char *publicId,const ACEXML_Char *systemId);
  92.  
  93. // Methods inherit from ACEXML_ErrorHandler.
  94.  
  95. /*
  96. * Receive notification of a recoverable error.
  97. */
  98. virtual void error (ACEXML_SAXParseException &exception);
  99.  
  100. /*
  101. * Receive notification of a non-recoverable error.
  102. */
  103. virtual void fatalError (ACEXML_SAXParseException &exception);
  104.  
  105. /*
  106. * Receive notification of a warning.
  107. */
  108. virtual void warning (ACEXML_SAXParseException &exception);
  109. private:
  110. ACEXML_Char* fileName_;
  111. ACEXML_Locator* locator_;
  112. private:
  113. CTree *m_ptree;
  114. CStack m_stack;
  115. string m_value;
  116. };
  117.  
  118.  
  119. #include "XMLParse.h"
  120. #include "ace/ACE.h"
  121. #include "ace/Log_Msg.h"
  122.  
  123. CXMLParse::CXMLParse (ACEXML_Char* fileName) : fileName_(ACE::strnew(fileName))
  124. {
  125. m_ptree = NULL;
  126. m_value = "";
  127. }
  128.  
  129. CXMLParse::~CXMLParse (void)
  130. {
  131. delete[] this->fileName_;
  132. if (m_ptree != NULL)
  133. {
  134. m_ptree->destroy();
  135. }
  136. }
  137.  
  138.  
  139. void CXMLParse::characters(const ACEXML_Char *cdata,size_t length)
  140. {
  141. m_value = string(cdata);
  142. }
  143.  
  144. void CXMLParse::startElement(const ACEXML_Char *uri,const ACEXML_Char *name,ACEXML_Attributes *alist)
  145. {
  146. CTree* node = new CTree(CTree::NOTLEAF,name);
  147. m_stack.push(node);
  148. }
  149.  
  150. void CXMLParse::endElement(const ACEXML_Char *uri,const ACEXML_Char *qName)
  151. {
  152. CTree* node = m_stack.get();
  153. if (node == NULL) return;
  154.  
  155. if (node->getleft() == NULL && node->getright() == NULL)//如果栈顶的元素左右没有孩子,说明需要插入节点
  156. {
  157. //生成叶子节点
  158. CTree* newnode = new CTree(CTree::LEAF,m_value);
  159. //叶子节点的父亲节点出栈
  160. node = m_stack.pop();
  161. //设置叶子节点和父亲节点的关系
  162. node->addnode(newnode);
  163. CTree* parentnode = m_stack.get();
  164. if (parentnode == NULL) return;
  165. parentnode->addnode(node);//记录相互关系,但不用出栈
  166. }
  167. else////如果栈顶的元素左右有孩子,说明仅仅需要修改指针关系
  168. {
  169. CTree* newnode = m_stack.pop();
  170. if (m_stack.empty())
  171. {
  172. m_ptree = newnode;
  173. }
  174. else
  175. {
  176. CTree* node = m_stack.get();
  177. node->addnode(newnode);
  178. }
  179. }
  180. }
  181.  
  182.  
  183. void CXMLParse::endDocument(void)
  184. {
  185. //test for display tree
  186. m_ptree->output(0);
  187. //test for get children number
  188. printf("*********************************************\n");
  189. printf("childnumber = %d\n",m_ptree->getchildnumber());
  190. printf("*********************************************\n");
  191.  
  192. //test for scan a parent's all children
  193. CTree* node = m_ptree->getfirstchild();
  194. while(node != NULL)
  195. {
  196. printf("value=%s\n",node->getvalue().c_str());
  197. node = node->getnextchild();
  198. }
  199.  
  200. //test for scan a parent's children
  201. printf("*********************************************\n");
  202. for(int i = 0; i < m_ptree->getchildnumber(); i++)
  203. {
  204. CTree* node = m_ptree->getchild(i + 1);
  205. if (node != NULL)
  206. {
  207. printf("value=%s\n",node->getvalue().c_str());
  208. }
  209. }
  210.  
  211. }
  212.  
  213. void CXMLParse::endPrefixMapping (const ACEXML_Char *prefix)
  214. {
  215.  
  216. }
  217.  
  218. void CXMLParse::ignorableWhitespace(const ACEXML_Char *,int,int)
  219. {
  220. }
  221.  
  222. void CXMLParse::processingInstruction(const ACEXML_Char *target,const ACEXML_Char *data)
  223. {
  224. }
  225.  
  226. void CXMLParse::setDocumentLocator(ACEXML_Locator * locator)
  227. {
  228. this->locator_ = locator;
  229. }
  230.  
  231. void CXMLParse::skippedEntity(const ACEXML_Char *name)
  232. {
  233. }
  234.  
  235. void CXMLParse::startDocument(void)
  236. {
  237. }
  238.  
  239. void CXMLParse::startPrefixMapping(const ACEXML_Char * prefix,const ACEXML_Char * uri)
  240. {
  241. }
  242.  
  243.  
  244. void CXMLParse::notationDecl(const ACEXML_Char *name,const ACEXML_Char *publicID,const ACEXML_Char *systemID)
  245. {
  246. }
  247.  
  248. void CXMLParse::unparsedEntityDecl(const ACEXML_Char *name,const ACEXML_Char *systemID,const ACEXML_Char *notationName)
  249. {
  250. }
  251.  
  252.  
  253. ACEXML_InputSource * CXMLParse::resolveEntity (const ACEXML_Char *,const ACEXML_Char *)
  254. {
  255. return 0;
  256. }
  257.  
  258. void CXMLParse::error(ACEXML_SAXParseException & ex)
  259. {
  260. /*ACE_DEBUG((LM_DEBUG,"%s: line: %d col: %d ",(this->locator_->getSystemId() == 0 ? this->fileName_ : this->locator_->getSystemId()),this->locator_->getLineNumber(),this->locator_->getColumnNumber()));
  261. ex.print();*/
  262. }
  263.  
  264. void CXMLParse::fatalError(ACEXML_SAXParseException& ex)
  265. {
  266. /*ACE_DEBUG ((LM_DEBUG,this->locator_->getColumnNumber()));
  267. ex.print();*/
  268.  
  269. }
  270.  
  271. void CXMLParse::warning(ACEXML_SAXParseException & ex)
  272. {
  273. /*ACE_DEBUG ((LM_DEBUG,this->locator_->getColumnNumber()));
  274. ex.print();*/
  275. }



CDecodeXML


  1. #pragma once
  2.  
  3. #include "string"
  4.  
  5. using namespace std;
  6.  
  7. class CDecodeXML
  8. {
  9. public:
  10. CDecodeXML(void);
  11. ~CDecodeXML(void);
  12. public:
  13. bool decode(string xmlfilename);
  14. };
  15.  
  16.  
  17. #include "DecodeXML.h"
  18. #include "ACEXML/common/FileCharStream.h"
  19. #include "ACEXML/parser/parser/Parser.h"
  20. #include "XMLParse.h"
  21. #include "ace/Auto_Ptr.h"
  22.  
  23.  
  24. CDecodeXML::CDecodeXML(void)
  25. {
  26. }
  27.  
  28.  
  29. CDecodeXML::~CDecodeXML(void)
  30. {
  31. }
  32.  
  33.  
  34. bool CDecodeXML::decode(string xmlfilename)
  35. {
  36. int result = true;
  37.  
  38. ACEXML_Char* filename = (ACEXML_Char*)xmlfilename.c_str();
  39. ACEXML_FileCharStream *fstm = 0;
  40. ACE_NEW_RETURN(fstm,ACEXML_FileCharStream(),!result);
  41.  
  42. if (fstm->open(filename) != 0)
  43. {
  44. printf("Failed to open XML file: %s\n",filename);
  45. result = false;
  46. return result;
  47. }
  48.  
  49. ACEXML_CharStream *stm = fstm;
  50. CXMLParse *handler = 0;
  51. ACE_NEW_RETURN(handler,CXMLParse(filename),!result);
  52. auto_ptr<ACEXML_DefaultHandler> cleanup_handler(handler);
  53.  
  54. ACEXML_Parser parser;
  55. ACEXML_InputSource input(stm);
  56.  
  57. parser.setContentHandler(handler);
  58. parser.setDTDHandler(handler);
  59. parser.setErrorHandler(handler);
  60. parser.setEntityResolver(handler);
  61.  
  62. try
  63. {
  64. parser.parse(&input);
  65. }
  66. catch (const ACEXML_SAXException& ex)
  67. {
  68. ex.print();
  69. printf("Exception occurred. Exiting...\n");
  70. result = false;
  71. }
  72.  
  73. return result;
  74. }
  75.  
  76.  


函数测试:


  1. #include "stdafx.h"
  2. #include "DecodeXML.h"
  3.  
  4.  
  5. int main(int argc,char* argv[])
  6. {
  7. CDecodeXML decodexml;
  8. decodexml.decode("d:\\xml\\1.xml");
  9. getchar();
  10. return 0;
  11. }

猜你在找的XML相关文章