xerces C/C++ 读取XML文件的一个小例子

main.cpp




#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/parsers/XercesDOMParser.hpp>
#include <xercesc/dom/DOM.hpp>
#include <xercesc/sax/HandlerBase.hpp>
#include <xercesc/util/XMLString.hpp>
#include <iostream>

// Other include files, declarations, and non-Xerces-C++ initializations.
#pragma comment (lib,"xerces-c_3.lib")
using namespace xercesc;
using namespace std;


int main()
{
	try {
		XMLPlatformUtils::Initialize();
	}
	catch (const XMLException& toCatch) {
		// Do your failure processing here
		cout << "Initialize Error!" << endl;
		return 1;
	}

	// Do your actual work with Xerces-C++ here.

	XercesDOMParser* parser = new XercesDOMParser();
	parser->setValidationScheme(XercesDOMParser::Val_Always);
	parser->setDoNamespaces(true);    // optional

	ErrorHandler* errHandler = (ErrorHandler*) new HandlerBase();
	parser->setErrorHandler(errHandler);

	//此处请用绝对路径
	char* xmlFile = "C:\\Users\\hp1\\Documents\\visual studio 2013\\Projects\\XMLParser\\Debug\\book.xml";

	try {
		parser->parse(xmlFile);
	}
	catch (const XMLException& toCatch) {
		char* message = XMLString::transcode(toCatch.getMessage());
		cout << "Exception message is: \n"
			<< message << "\n";
		XMLString::release(&message);
		return -1;
	}
	catch (const DOMException& toCatch) {
		char* message = XMLString::transcode(toCatch.msg);
		cout << "Exception message is: \n"
			<< message << "\n";
		XMLString::release(&message);
		return -2;
	}
	catch (const SAXException& toCatch) {
		char* message = XMLString::transcode(toCatch.getMessage());
		cout << "Exception message is: \n"
			<< message << "\n";
		XMLString::release(&message);
		return -3;
	}
	catch (...) {
		cout << "Unexpected Exception \n";
		return -4;
	}



	//读取


	DOMDocument* doc = parser->getDocument();
	DOMNodeList* nodelist = doc->getChildNodes();
	DOMNode* root;
	DOMNodeList* childlist;
	DOMNode* child;
	DOMNode* attr;
	DOMNodeList* attrs;
	//最外层的 books
	for (int i = 0; i < nodelist->getLength(); i++)
	{
		root = nodelist->item(i);
		char *nodename = XMLString::transcode(root->getNodeName());
		cout << nodename << endl;
		XMLString::release(&nodename);
		if (root->hasChildNodes())
		{
			childlist = root->getChildNodes();
			//book
			for (int j = 1; j < childlist->getLength()-1; j+=2)
			{
				child = childlist->item(j);
				char *childname = XMLString::transcode(child->getNodeName());
				cout << "	" << childname << endl;
				XMLString::release(&childname);

				if (child->hasChildNodes())
				{
					attrs = child->getChildNodes();
					//name 等等
					for (int k = 1; k < attrs->getLength()-1; k+=2)
					{
						attr = attrs->item(k);
						char *attrname = XMLString::transcode(attr->getNodeName());
						char *attrvalue = XMLString::transcode(attr->getTextContent());

						cout << "		" << attrname << " : " << attrvalue << endl;
						XMLString::release(&attrname);
						XMLString::release(&attrvalue);

					}
				}

			}
		}
		
	}

	//
	int stop;
	cin >> stop;

	delete parser;
	delete errHandler;


	XMLPlatformUtils::Terminate();

	// Other terminations and cleanup.
	return 0;
}





book.xml




<?xml version="1.0" encoding="GB2312" standalone="no"?>
<books>
    <book>
        <name>book1</name>
        <price>1.1</price>
    </book>
	
	<book>
        <name>book2</name>
        <price>2.2</price>
    </book>
	
	<book>
        <name>book3</name>
        <price>3.3</price>
    </book>
</books>


展开阅读全文

没有更多推荐了,返回首页