Xerces C ++ sax Parse XML documents

Source: Internet
Author: User

About the explanation of the sax, and the benefits, do not too much introduction, you can find a vast number of search engine inside the introduction of http://baike.soso.com/v123641.htm? Ch = CH. bk. innerlink;

This document describes how to use xerces C ++ Sax to parse XML documents in your c ++ program;

1. xerces C ++ download

First, we need to download xerces C ++: http://xerces.apache.org/xerces-c/download.cgi

The above Connection provides the xerces C ++ compilation files and source code for various vs versions for you to download and use.

2. Introduce xerces C ++ in the project

1. C/C ++: attachment inclusion Directory: Introduce include; header file

2. In general, the linker contains the attachment Directory: introducing LIB; bin; File

3. In the linker input: Additional dependency: Import: xerces-c_3.lib xerces-c_static_3.lib

3. OK. After the above environment is set up, start coding.

#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 "MySAX2Handler.h"using namespace std;using namespace xercesc_3_1;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 = "x1.xml";SAX2XMLReader* parser = XMLReaderFactory::createXMLReader();parser->setFeature(XMLUni::fgSAX2CoreValidation, true);parser->setFeature(XMLUni::fgSAX2CoreNameSpaces, true);   // optionalparser->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;}

The Declaration of the main () function, including the declaration of the SAX Parser.

4. You need to implement your own interfaces:

#pragma once#include<xercesc/sax2/DefaultHandler.hpp>#include <xercesc/sax2/Attributes.hpp>#include "PathsynopsisNode.h"using namespace std;using namespace xercesc_3_1;class MySAX2Handler :public DefaultHandler{public:MySAX2Handler(void);~MySAX2Handler(void);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&);};

5. If the XML data parsed by sax contains Chinese characters, Sax directly enters the fatalError event, leading to parsing errors.

We need to add the document encoding method at the beginning of the XML document: <? XML version = "1.0" encoding = "gb2312"?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.