今天学习一下用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 <

return;

}

catch (const XMLException& e)

{

XERCES_STD_QUALIFIER cerr <

return;

}

catch (const DOMException& e)

{

constunsigned intmaxChars = 2047;

XMLCh errText[maxChars + 1];

XERCES_STD_QUALIFIER cerr <

if(DOMImplementation::loadDOMExceptionMsg(e.code,errText,maxChars))

XERCES_STD_QUALIFIER cerr<< "Message is: " <<:transcode xerces_std_qualifier endl>

return;

}

catch(...)

{

XERCES_STD_QUALIFIER cerr <

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<<:transcode>item(i)->getNodeName())<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<<:transcode>getNodeName())<

}

else

{

cout<<:transcode>getNodeName())<getTextContent())<

}

}

}

}

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(pChild);

cout<<:transcode>getTagName())<getTextContent())<

}

}

}

//用DOMTreeWalker实现遍历XML

voidXDomParser::PrintByTreeWalker(DOMNode *pNode)

{

cout<<:transcode>(pNode)->getTagName())<

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(pChild);

if (pElement->getChildElementCount())

{

PrintByTreeWalker(pChild);

}

else

{

cout<<:transcode>getTagName())<getTextContent())<

}

}

}

}

相关文章

总结

以上是编程之家为你收集整理的Xerces解析XML全部内容,希望文章能够帮你解决Xerces解析XML所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。

如您喜欢交流学习经验,点击链接加入交流1群:1065694478(已满)交流2群:163560250

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐