如下是一个XML文件:sample.xml <?xml version="1.0" encoding="utf-8"?> <常用XML解析库> <库1>Xerces-C</库1> <库2>libxml2</库2> <库3>expat</库3> <库4>tinyxml</库4> <库5>msxml</库5> </常用XML解析库> 如下是解析此文件的源代码: #include <windows.h> #include <xercesc/dom/DOM.hpp> #include <xercesc/dom/DOMDocument.hpp> #include <xercesc/dom/DOMDocumentType.hpp> #include <xercesc/dom/DOMElement.hpp> #include <xercesc/dom/DOMImplementation.hpp> #include <xercesc/dom/DOMImplementationLS.hpp> #include <xercesc/dom/DOMNodeIterator.hpp> #include <xercesc/dom/DOMNodeList.hpp> #include <xercesc/dom/DOMText.hpp> #include <xercesc/parsers/XercesDOMParser.hpp> #include <xercesc/util/XMLUni.hpp> #include <iostream> using namespace std; #ifdef _DEBUG #pragma comment( lib, "xerces-c_2d.lib" ) #else #pragma comment( lib, "xerces-c_2.lib" ) #endif class CXMLNodeContent { private: char* m_pNodeText; int m_BufferSize; int m_CurBufferSize; public: CXMLNodeContent() { m_pNodeText = NULL; m_BufferSize = 0; m_CurBufferSize = 0; } ~CXMLNodeContent() { if( m_pNodeText != NULL ) { delete []m_pNodeText; m_pNodeText = NULL; } m_BufferSize = 0; m_CurBufferSize = 0; } int printNodeContent( const XMLCh* pText ) { getNodeContent( pText ); if( m_CurBufferSize == 1 ) return 0; else { printf( "%s", m_pNodeText ); return 1; } } int getBufferSize() { return m_CurBufferSize; } char* getNodeContent( const XMLCh* pText ) { if( pText == NULL ) { m_CurBufferSize = 0; return NULL; } int index = 0; m_CurBufferSize = WideCharToMultiByte(CP_ACP, 0 ,pText, -1 , 0 , 0, NULL, NULL); if( pText[0] == 10 ) { while( pText[index] == 10 || pText[index] == 32 && index < m_CurBufferSize ) index++; if( index == m_CurBufferSize ) { m_CurBufferSize = 0; return NULL; } } if( m_CurBufferSize > m_BufferSize ) { if( m_pNodeText == NULL ) { m_pNodeText = new char[m_CurBufferSize]; m_BufferSize = m_CurBufferSize; } else { delete []m_pNodeText; m_pNodeText = new char[m_CurBufferSize]; m_BufferSize = m_CurBufferSize; } } WideCharToMultiByte(CP_ACP, 0 ,pText + index, -1 , m_pNodeText , m_CurBufferSize, NULL, NULL); return m_pNodeText; } }; int main() { CXMLNodeContent nodeProxy; xercesc::XMLPlatformUtils::Initialize(); xercesc::XercesDOMParser* myParser = new xercesc::XercesDOMParser; myParser->setValidationScheme( xercesc::XercesDOMParser::Val_Auto ); myParser->setDoNamespaces( false ); myParser->setDoSchema( false ); myParser->setLoadExternalDTD( false ); myParser->parse( "sample.xml" ); xercesc::DOMDocument* xmlDoc = myParser->getDocument(); xercesc::DOMElement* elementRoot = xmlDoc->getDocumentElement(); if( elementRoot == NULL ) { xercesc::XMLPlatformUtils::Terminate(); cout << "空文档或文档格式错误!" << endl; return 0; } nodeProxy.printNodeContent( elementRoot->getNodeName() ); cout<<endl; xercesc::DOMNodeList* children = elementRoot->getChildNodes(); XMLSize_t nodeCount = children->getLength(); for( XMLSize_t xx = 0; xx < nodeCount; xx++ ) { xercesc::DOMNode* currentNode = children->item(xx); if( currentNode->getNodeType() && currentNode->getNodeType() == xercesc::DOMNode::ELEMENT_NODE ) { xercesc::DOMElement* currentElement = (xercesc::DOMElement*)(currentNode); cout<<"\t"; nodeProxy.printNodeContent( currentElement->getTagName() ); cout<<"\t"; nodeProxy.printNodeContent(currentNode->getTextContent()); cout<<endl; } } cout<<"\n解析完毕"<<endl; xercesc::XMLPlatformUtils::Terminate(); getchar(); return 0; }