XML Schema validation with Xerces-c

Source: Internet
Author: User

On Xerces-c's official site, the An article guide explains how XML Schema validation is done.

Http://xerces.apache.org/xerces-c/schema-3.html

The sample code given:

Instantiate the DOM parser. Xercesdomparser Parser;parser.setdonamespaces (True);p Arser.setdoschema (True);p arser.parse (xmlfile);

But. The sample code has no effect whatsoever.

There are two things to do before calling Xercesdomparser::p arse:

1. call Xercesdomparser::setvalidationscheme to set the calibration schedule

Parser.setvalidationscheme (Xercesdomparser::val_auto);

Or

Parser.setvalidationscheme (xercesdomparser::val_always);

2. to invoke Xercesdomparser::seterrorhandler, the parameter must be an object of the ErrorHandler class or subclass.


Look at the following examples

Address.xml:

<?xml version= "1.0" encoding= "Utf-8"?

><address xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:nonamespaceschemalocation= " Address.xsd "><recipient>mr Walter c. brown</recipient>


Address.xsd:

<?xml version= "1.0" encoding= "Utf-8"?

><xs:schema elementformdefault= "qualified" xmlns:xs= "Http://www.w3.org/2001/XMLSchema" ><xs:element Name= "Address" ><xs:complextype><xs:sequence><xs:element name= "Recipient" type= "xs:string"/> <xs:element name= "House" type= "xs:string"/><xs:element name= "Street" type= "xs:string"/><xs:element Name= "Town" type= "xs:string"/><xs:element name= "county" type= "xs:string" minoccurs= "0"/><xs:element Name= "postcode" type= "Xs:unsignedint"/><xs:element name= "Country" minoccurs= "0" ><xs:simpleType> <xs:restriction base= "xs:string" ><xs:enumeration value= "in"/><xs:enumeration value= "DE"/><xs : Enumeration value= "ES"/><xs:enumeration value= "UK"/><xs:enumeration value= "US"/></XS: RESTRICTION></XS:SIMPLETYPE></XS:ELEMENT></XS:SEQUENCE></XS:COMPLEXTYPE></XS: Element></xs:schema>


New_address.cpp:

#include <stdio.h> #include <xercesc/parsers/XercesDOMParser.hpp> #include <xercesc/sax/ saxexception.hpp> #include <xercesc/dom/DOMException.hpp> #include <xercesc/dom/domelement.hpp># Include <xercesc/dom/DOMLSException.hpp> #include <xercesc/sax2/defaulthandler.hpp>using namespace Xerces_cpp_namespace;class schemaerrorhandler:public Defaulthandler{public:schemaerrorhandler () {}~ Schemaerrorhandler () {}void warning (const saxparseexception& exc) {printf ("Warn in Line:%lu, Col:%lu,%s\n",    Exc.getlinenumber (), Exc.getcolumnnumber (), Xmlstring::transcode (Exc.getmessage ()));} void error (const saxparseexception& exc) {printf ("Error in Line:%lu, Col:%lu,%s\n", Exc.getlinenumber (),    Exc.getcolumnnumber (), Xmlstring::transcode (Exc.getmessage ()));} void FatalError (const saxparseexception& exc) {printf ("Fatal in Line:%lu, Col:%lu,%s\n", Exc.getlinenumber (),    Exc.getcolumnnumber (), Xmlstring::transcode (Exc.getmessage ()));} void ReseterrORS () {printf ("nothing\n");}}; int main (int argc, char* argv[]) {if (ARGC < 2) {printf ("must specify a file\n"); return-1;} Xmlplatformutils::initialize (); Xercesdomparser parser; Schemaerrorhandler Handler;try{parser.seterrorhandler (&handler);p arser.setdonamespaces (true); Parser.setdoschema (True);//parser.setvalidationscheme (Xercesdomparser::val_auto);p arser.parse (argv[1]);} catch (saxexception& e) {printf ("msg:%s\n", Xmlstring::transcode (E.getmessage ())); return-2;} catch (xmlexception& e) {printf ("code:%d, msg:%s\n", E.getcode (), Xmlstring::transcode (E.getmessage ())); return-3 ;} catch (domexception& e) {printf ("code:%d, msg:%s\n", E.code, e.msg); return-4;} return 0;}
Can see the code here staring out of this line:

Parser.setvalidationscheme (Xercesdomparser::val_auto);


Compile execution:

Put

Parser.setvalidationscheme (Xercesdomparser::val_auto);
Open, but stare away

Parser.seterrorhandler (&handler);
Compile execution:


Put

Parser.seterrorhandler (&handler);

Open, compile execution:

[[email protected] sample]$./new_address address.xml Error in Line:8, col:31, value ' ec1y 8SY ' does not match regular exp ression facet ' [+\-]? [0-9]+ '

Take a look at the Xmllint and compare the results:

[Email protected] sample]$ xmllint--schema address.xsd address.xml<?xml version= "1.0" encoding= "Utf-8"?>< Address xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:nonamespaceschemalocation= "address.xsd" > <recipient>mr Walter c. brown</recipient>

Ps:

In XML Schema, string is compatible with other types, such as the contents of the house tag to write a number, for example, 49, regardless of xmllint, or xerces will not report the value of this tag is problematic.

Used to toss an afternoon for this problem.

XML Schema validation with Xerces-c

Related Article

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.