今天学习一下用XercesDOMParser 解析XML。简单的实现了自定义的类XdomParser,分别用DOMNodeIterator和DOMTreeWalker实现对DOM树的遍历输出。这里仅是为了简单理解XercesDOMParser的用法,至于它具体支持的属性以后再仔细研究。
class XDomParser
{
public:
XDomParser(string xmlFilePath = "");
~XDomParser();
voidParser(string xmlFilePath = "");
voidPrint();
voidPrint(DOMElement *pElement);
voidPrintByIterator(DOMNode *pNode);
voidPrintByTreeWalker(DOMNode *pNode);
DOMElement *GetRootElement();
protected:
private:
string m_filePath;
XercesDOMParser *m_pParser;
DOMElement * m_pRoot;
};
XDomParser::XDomParser(string xmlFilePath /*= ""*/):m_filePath(xmlFilePath)
{
m_pParser = newXercesDOMParser;
}
XDomParser::~XDomParser()
{
deletem_pParser;
}
//解析XML
void XDomParser::Parser(string xmlFilePath /*= ""*/ )
{
try
{
if(m_filePath.length())
{
m_pParser->parse(m_filePath.c_str());
}
elseif(xmlFilePath.length())
{
m_pParser->parse(xmlFilePath.c_str());
}
}
catch (const OutOfMemoryException&)
{
XERCES_STD_QUALIFIER cerr <<"OutOfMemoryException" <<XERCES_STD_QUALIFIER endl;
return;
}
catch (const XMLException& e)
{
XERCES_STD_QUALIFIER cerr <<"An error occurred during parsing\n Message: "<<XERCES_STD_QUALIFIERendl;
return;
}
catch (const DOMException& e)
{
constunsigned intmaxChars = 2047;
XMLCh errText[maxChars + 1];
XERCES_STD_QUALIFIER cerr <<"\nDOM Error during parsing: '"<< m_filePath << "'\n"<<endl;
if(DOMImplementation::loadDOMExceptionMsg(e.code,errText,maxChars))
XERCES_STD_QUALIFIER cerr<< "Message is: " <<XMLString::transcode(errText) << XERCES_STD_QUALIFIER endl;
return;
}
catch(...)
{
XERCES_STD_QUALIFIER cerr <<"An error occurred during parsing\n "<< XERCES_STD_QUALIFIER endl;
return;
}
DOMDocument *pdoc =m_pParser->getDocument();
m_pRoot = pdoc->getDocumentElement(); //获取XML根节点
}
void XDomParser::Print()
{
if(m_pRoot)
{
if(m_pRoot)
{
DOMNodeList*pVersionEleList = m_pRoot->getElementsByTagName(XMLString::transcode("Name")); //获取Name节点
for(int i = 0; i < pVersionEleList->getLength();++i)
{
DOMNode *pNode =pVersionEleList->item(i);
cout<<XMLString::transcode(pVersionEleList->item(i)->getNodeName())<<" value :"<<XMLString::transcode(pVersionEleList->item(i)->getTextContent());
}
}
}
}
//输出pElement及其子节点
void XDomParser::Print(DOMElement *pElement )
{
if(pElement)
{
DOMElement *pChild =pElement->getFirstElementChild();
for(pChild ; pChild ;pChild = pChild->getNextElementSibling())
{
intchildCount = pChild->getChildElementCount();
string tagName =XMLString::transcode(pChild->getNodeName());
if(pChild->getChildElementCount())
{
cout<<XMLString::transcode(pChild->getNodeName())<<endl; Print(pChild);
}
else
{
cout<<XMLString::transcode(pChild->getNodeName())<<" value :"<<XMLString::transcode(pChild->getTextContent())<<endl;
}
}
}
}
DOMElement* XDomParser::GetRootElement()
{
returnm_pRoot;
}
//用DOMNodeIterator实现遍历XML
voidXDomParser::PrintByIterator(DOMNode *pNode)
{
DOMDocument *pdoc =m_pParser->getDocument();
DOMNodeIterator*pIterator=pdoc->createNodeIterator(pNode,DOMNodeFilter::SHOW_ALL,NULL,true);
DOMNode *pChild =pIterator->getRoot();
for(pChild; pChild; pChild = pIterator->nextNode())
{
string tagName =XMLString::transcode(pChild->getNodeName());
if((pChild->getNodeType()== DOMNode::ELEMENT_NODE))
{
DOMElement* pElement = static_cast<DOMElement*>(pChild);
cout<<XMLString::transcode(pElement->getTagName())<<" value :"<<XMLString::transcode(pElement->getTextContent())<<endl;;
}
}
}
//用DOMTreeWalker实现遍历XML
voidXDomParser::PrintByTreeWalker(DOMNode *pNode)
{
cout<<XMLString::transcode(static_cast<DOMElement*>(pNode)->getTagName())<<endl;
DOMDocument *pdoc =m_pParser->getDocument();
string tagName =XMLString::transcode(m_pRoot->getNodeName());
DOMTreeWalker*pInnerWalker=pdoc->createTreeWalker(pNode,true);
DOMNode *pChild =pInnerWalker->firstChild();
pChild = pInnerWalker->nextNode();
for(pChild; pChild; pChild = pInnerWalker->nextNode())
{
string tagName =XMLString::transcode(pChild->getNodeName());
if((pChild->getNodeType() ==DOMNode::ELEMENT_NODE))
{
DOMElement* pElement= static_cast<DOMElement*>(pChild);
if (pElement->getChildElementCount())
{
PrintByTreeWalker(pChild);
}
else
{
cout<<XMLString::transcode(pElement->getTagName())<<" value :"<<XMLString::transcode(pElement->getTextContent())<<endl;;
}
}
}
}
原文链接:https://www.f2er.com/xml/299141.html