xercesc 解析xml获取错误
作者:互联网
xml内容:
<?xml version="1.0" encoding="utf-8"?>
<Address>
<Recipient>Mr. Walter C. Brown</Recipient>
<House>49</House>
<Street>Featherstone Street</Street>
<Town>LONDON</Town>
<PostCode>EC1Y 8SY</PostCode>
<Country>UK</Country>
</Address>
(1)LPDocument.h
#pragma once
#include <xercesc/dom/DOM.hpp>
#include <xercesc/parsers/XercesDOMParser.hpp>
#include <xercesc/sax/HandlerBase.hpp>
#include <string>
using namespace xercesc;
using namespace std;
class LPDocument
{
public:
LPDocument();
~LPDocument();
bool ParseDocument(const char* strPath);
xercesc::DOMDocument* m_pDoc;
string strErrorMsg;
private:
XercesDOMParser *parser;
ErrorHandler* pErrHandler;
};
(2)LPDocument.cpp
#include "stdafx.h"
#include "LPDocument.h"
#include <xercesc/dom/DOM.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 <xercesc/dom/DOMNode.hpp>
#include <xercesc/parsers/SAXParser.hpp>
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/util/XMLString.hpp>
#include <xercesc/util/OutOfMemoryException.hpp>
#include <xercesc/sax/HandlerBase.hpp>
using namespace xercesc;
LPDocument::LPDocument()
{
m_pDoc = nullptr;
}
LPDocument::~LPDocument()
{
if (parser != nullptr)
{
delete parser;
parser = nullptr;
}
if (pErrHandler != nullptr)
{
delete pErrHandler;
pErrHandler = nullptr;
}
}
bool LPDocument::ParseDocument(const char* strPath)
{
XMLPlatformUtils::Initialize();
parser = new XercesDOMParser;
pErrHandler = (ErrorHandler*) new HandlerBase();
try
{
parser->setDoNamespaces(true);
parser->setValidationScheme(XercesDOMParser::Val_Always);
parser->setErrorHandler(pErrHandler);
parser->parse(strPath);
}
catch (SAXException& e)
{
char *strMsg = XMLString::transcode(e.getMessage());
strErrorMsg = strMsg;
XMLString::release(&strMsg);
return false;
}
catch (XMLException& e)
{
char *strMsg = XMLString::transcode(e.getMessage());
strErrorMsg = strMsg;
XMLString::release(&strMsg);
return false;
}
catch (DOMException& e)
{
char *strMsg = XMLString::transcode(e.getMessage());
strErrorMsg = strMsg;
XMLString::release(&strMsg);
return false;
}
m_pDoc = parser->getDocument();
return true;
}
主函数解析:
#include "LPDocument.h"
int main( int argc, char **argv)
{
LPDocument docu;
if (!docu.ParseDocument("2.xml"))
{
}
xercesc::DOMDocument* pDoc = docu.m_pDoc;
if (pDoc == nullptr)
{
string strMsg = docu.strErrorMsg;
return -1;
}
DOMElement* root = pDoc->getDocumentElement();
if (root == NULL)
{
return -1;
}
DOMElement* child = root->getFirstElementChild();
if (child != nullptr)
{
XMLSize_t cnt = root->getChildElementCount();
for (XMLSize_t i = 0; i < cnt; i++)
{
char* name = XMLString::transcode(child->getTagName());
printf("%s=", name);
XMLString::release(&name);
char* value = XMLString::transcode(child->getTextContent());
printf("%s\n", value);
XMLString::release(&value);
child = child->getNextElementSibling();
}
}
return 0;
}
标签:xml,解析,xercesc,XMLString,parser,strMsg,char,LPDocument,include 来源: https://blog.csdn.net/u011269801/article/details/120387900