myfile.xml
<level1>
<level2>
<name>abc</name>
</level2>
<level2>
<name>xyz</name>
</level2>
</level1>
#include < xercesc / util / PlatformUtils.hpp >
#include < xercesc / framework / XMLFormatter.hpp >
#include < xercesc / util / XMLString.hpp >
#include < xercesc / util / XMLUniDefs.hpp >
#include < xercesc / framework / MemBufInputSource.hpp >
#include < xercesc / framework / LocalFileInputSource.hpp >
#include < xercesc / parsers / XercesDOMParser.hpp >
#include < xercesc / dom / DOMNode.hpp >
#include < xercesc / dom / DOMNodeList.hpp >
#include < stdio.h >
#include < iostream >
#include < vector >
#include < string >
using namespace XERCES_CPP_NAMESPACE;
using namespace std;
int main()
{
/* initialise xerces */
try
{
XMLPlatformUtils::Initialize ();
}
catch ( const XMLException & toCatch)
{
char * pMsg = XMLString::transcode (toCatch.getMessage ());
printf ( " Error during Xerces-c Initialization.\n "
" Exception message: %s " , pMsg);
delete[] pMsg;
return - 1 ;
}
/* parse a XML file */
XercesDOMParser * m_DOMParser = new XercesDOMParser();
m_DOMParser -> setValidationScheme(XercesDOMParser::Val_Always);
// ErrorHandler *m_ERRHandler = (ErrorHandler*)new HandlerBase();
// m_DOMParser->setErrorHandler(m_ERRHandler);
try
{
m_DOMParser -> parse( " myfile.xml " );
}
catch ( const XMLException & toCatch)
{
return - 1 ;
}
XMLCh temp[ 257 ];
DOMNode * root = m_DOMParser -> getDocument(); // 取得了整个树的根节点
DOMNodeList * childList = root -> getChildNodes(); // 获取所有子节点
for ( int j = 0 ; j < childList -> getLength(); ++ j)
{
XMLString::transcode( " level1 " , temp, 256 ); // 内部使用UTF-16编码,所以需要转换
if (childList -> item(j) -> getNodeType() == DOMNode::ELEMENT_NODE
&& XMLString::compareString(childList -> item(j) -> getNodeName(), temp) == 0 )
{
char * tn = XMLString::transcode(childList -> item(j) -> getNodeName());
std::cout << tn << std::endl;
DOMNode * subRoot = childList -> item(j);
DOMNodeList * subChildList = subRoot -> getChildNodes();
for ( int k = 0 ; k < subChildList -> getLength(); ++ k)
{
XMLString::transcode( " level2 " , temp, 256 ); // 内部使用UTF-16编码,所以需要转换
if (subChildList -> item(k) -> getNodeType() == DOMNode::ELEMENT_NODE
&& XMLString::compareString(subChildList -> item(k) -> getNodeName(), temp) == 0 )
{
char * tn = XMLString::transcode(subChildList -> item(k) -> getNodeName());
std::cout << " \t " << tn << std::endl;
DOMNode * ssubRoot = subChildList -> item(k);
DOMNodeList * ssubChildList = ssubRoot -> getChildNodes();
for ( int m = 0 ; m < ssubChildList -> getLength(); ++ m)
{
XMLString::transcode( " name " , temp, 256 ); // 内部使用UTF-16编码,所以需要转换
if (ssubChildList -> item(m) -> getNodeType() == DOMNode::ELEMENT_NODE
&& XMLString::compareString(ssubChildList -> item(m) -> getNodeName(), temp) == 0 )
{
char * tn = XMLString::transcode(ssubChildList -> item(m) -> getNodeName());
char * tv = XMLString::transcode(ssubChildList -> item(m) -> getFirstChild() -> getNodeValue()); // 每个ELEMENT的第一子节点为TEXT_NODE
std::cout << " \t\t " << tn << " : " << tv << std::endl; // tv=="abc",
XMLString::release( & tn);
XMLString::release( & tv);
}
}
}
}
}
}
XMLPlatformUtils::Terminate();
return 0 ;
}
<level1>
<level2>
<name>abc</name>
</level2>
<level2>
<name>xyz</name>
</level2>
</level1>
#include < xercesc / util / PlatformUtils.hpp >
#include < xercesc / framework / XMLFormatter.hpp >
#include < xercesc / util / XMLString.hpp >
#include < xercesc / util / XMLUniDefs.hpp >
#include < xercesc / framework / MemBufInputSource.hpp >
#include < xercesc / framework / LocalFileInputSource.hpp >
#include < xercesc / parsers / XercesDOMParser.hpp >
#include < xercesc / dom / DOMNode.hpp >
#include < xercesc / dom / DOMNodeList.hpp >
#include < stdio.h >
#include < iostream >
#include < vector >
#include < string >
using namespace XERCES_CPP_NAMESPACE;
using namespace std;
int main()
{
/* initialise xerces */
try
{
XMLPlatformUtils::Initialize ();
}
catch ( const XMLException & toCatch)
{
char * pMsg = XMLString::transcode (toCatch.getMessage ());
printf ( " Error during Xerces-c Initialization.\n "
" Exception message: %s " , pMsg);
delete[] pMsg;
return - 1 ;
}
/* parse a XML file */
XercesDOMParser * m_DOMParser = new XercesDOMParser();
m_DOMParser -> setValidationScheme(XercesDOMParser::Val_Always);
// ErrorHandler *m_ERRHandler = (ErrorHandler*)new HandlerBase();
// m_DOMParser->setErrorHandler(m_ERRHandler);
try
{
m_DOMParser -> parse( " myfile.xml " );
}
catch ( const XMLException & toCatch)
{
return - 1 ;
}
XMLCh temp[ 257 ];
DOMNode * root = m_DOMParser -> getDocument(); // 取得了整个树的根节点
DOMNodeList * childList = root -> getChildNodes(); // 获取所有子节点
for ( int j = 0 ; j < childList -> getLength(); ++ j)
{
XMLString::transcode( " level1 " , temp, 256 ); // 内部使用UTF-16编码,所以需要转换
if (childList -> item(j) -> getNodeType() == DOMNode::ELEMENT_NODE
&& XMLString::compareString(childList -> item(j) -> getNodeName(), temp) == 0 )
{
char * tn = XMLString::transcode(childList -> item(j) -> getNodeName());
std::cout << tn << std::endl;
DOMNode * subRoot = childList -> item(j);
DOMNodeList * subChildList = subRoot -> getChildNodes();
for ( int k = 0 ; k < subChildList -> getLength(); ++ k)
{
XMLString::transcode( " level2 " , temp, 256 ); // 内部使用UTF-16编码,所以需要转换
if (subChildList -> item(k) -> getNodeType() == DOMNode::ELEMENT_NODE
&& XMLString::compareString(subChildList -> item(k) -> getNodeName(), temp) == 0 )
{
char * tn = XMLString::transcode(subChildList -> item(k) -> getNodeName());
std::cout << " \t " << tn << std::endl;
DOMNode * ssubRoot = subChildList -> item(k);
DOMNodeList * ssubChildList = ssubRoot -> getChildNodes();
for ( int m = 0 ; m < ssubChildList -> getLength(); ++ m)
{
XMLString::transcode( " name " , temp, 256 ); // 内部使用UTF-16编码,所以需要转换
if (ssubChildList -> item(m) -> getNodeType() == DOMNode::ELEMENT_NODE
&& XMLString::compareString(ssubChildList -> item(m) -> getNodeName(), temp) == 0 )
{
char * tn = XMLString::transcode(ssubChildList -> item(m) -> getNodeName());
char * tv = XMLString::transcode(ssubChildList -> item(m) -> getFirstChild() -> getNodeValue()); // 每个ELEMENT的第一子节点为TEXT_NODE
std::cout << " \t\t " << tn << " : " << tv << std::endl; // tv=="abc",
XMLString::release( & tn);
XMLString::release( & tv);
}
}
}
}
}
}
XMLPlatformUtils::Terminate();
return 0 ;
}
g++ -g -O2 -msse2 -o DOMPrint src/DOMPrint/DOMPrint.o src/DOMPrint/DOMPrintErrorHandler.o src/DOMPrint/DOMPrintFilter.o src/DOMPrint/DOMTreeErrorReporter.o ../src/.libs/libxerces-c.so -lnsl -lpthread
参考:
1,http://panpan.blog.51cto.com/489034/187272
2,http://blog.csdn.net/wangpeng110m/archive/2009/05/14/4181668.aspx
遇到的错误:
1,xml文件本身有问题,结束标签忘记写“/“。