一、 vs2013使用Xerces C++ SAX解析XML文档
程序员文章站
2022-06-17 14:23:00
...
1、下载Xerces
链接:http://xerces.apache.org/xerces-c/download.cgi
由于下载的文件中没有动态链接库,需要我们手动进行编译
2. 编译动态链接库
使用工具cmake,使用方法参考之前博客
https://blog.csdn.net/z345436330/article/details/84888504
编译完成的动态链接库:
3. example
新建一个vscode工程,在工程中引入Xerces C++
a、C/C++ 常规中:附件包含目录:引入include ;头文件
b、链接器常规中:附件包含目录:引入lib;bin;文件
c、链接器输入中:附加依赖项:引入:xerces-c_3.lib
d、拷贝动态链接dll到项目目录下
// main.cpp
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/sax2/SAX2XMLReader.hpp>
#include <xercesc/sax2/DefaultHandler.hpp>
#include <xercesc/sax2/XMLReaderFactory.hpp>
#include <xercesc/util/XMLString.hpp>
#include <iostream>
#include "MySAXHandler.h"
using namespace std;
using namespace xercesc_3_2;
int main(int argc, char* args[]) {
try {
XMLPlatformUtils::Initialize();
}
catch (const XMLException& toCatch) {
char* message = XMLString::transcode(toCatch.getMessage());
cout << "Error during initialization! :\n";
cout << "Exception message is: \n"
<< message << "\n";
XMLString::release(&message);
return 1;
}
char* xmlFile = "meta.xml";
SAX2XMLReader* parser = XMLReaderFactory::createXMLReader();
parser->setFeature(XMLUni::fgSAX2CoreValidation, true);
parser->setFeature(XMLUni::fgSAX2CoreNameSpaces, true); // optional
parser->setFeature(XMLUni::fgXercesDynamic, false);
parser->setFeature(XMLUni::fgXercesSchema, true);
parser->setFeature(XMLUni::fgXercesSchemaFullChecking, true);
MySAX2Handler* defaultHandler = new MySAX2Handler();
parser->setContentHandler(defaultHandler);
parser->setErrorHandler(defaultHandler);
parser->setEntityResolver(defaultHandler);
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 SAXParseException& toCatch) {
char* message = XMLString::transcode(toCatch.getMessage());
cout << "Exception message is: \n"
<< message << "\n";
XMLString::release(&message);
return -1;
}
catch (...) {
cout << "Unexpected Exception \n";
return -1;
}
//defaultHandler->OutputPsList();
delete parser;
delete defaultHandler;
XMLPlatformUtils::Terminate();
system("PAUSE");
return 0;
}
// MySAXHandler.h
#pragma once
#include<xercesc/sax2/DefaultHandler.hpp>
#include <xercesc/sax2/Attributes.hpp>
using namespace std;
using namespace xercesc_3_2;
XERCES_CPP_NAMESPACE_USE
class MySAX2Handler :public DefaultHandler
{
public:
MySAX2Handler();
~MySAX2Handler();
public:
void startElement(
const XMLCh* const uri,
const XMLCh* const localname,
const XMLCh* const qname,
const Attributes& attrs
);
void characters(
const XMLCh* const chars
, const XMLSize_t length
);
void endElement(
const XMLCh* const uri,
const XMLCh* const localname,
const XMLCh* const qname
);
void fatalError(const SAXParseException&);
};
// MySAXHandler.cpp
#include "MySAXHandler.h"
#include <iostream>
using namespace std;
MySAX2Handler::MySAX2Handler()
{
}
MySAX2Handler::~MySAX2Handler() {
}
void MySAX2Handler::startElement(
const XMLCh* const uri,
const XMLCh* const localname,
const XMLCh* const qname,
const Attributes& attrs
)
{
char* message = XMLString::transcode(qname);
cout << "I saw element: " << message << endl;
XMLString::release(&message);
}
void MySAX2Handler::fatalError(const SAXParseException& exception)
{
char* message = XMLString::transcode(exception.getMessage());
cout << "Fatal Error: " << message
<< " at line: " << exception.getLineNumber()
<< endl;
XMLString::release(&message);
}
void MySAX2Handler::characters(
const XMLCh* const chars
, const XMLSize_t length
) {
}
void MySAX2Handler::endElement(
const XMLCh* const uri,
const XMLCh* const localname,
const XMLCh* const qname
) {
}