用xerces-c来进行xml schema校验

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用xerces-c来进行xml schema校验相关的知识,希望对你有一定的参考价值。

在xerces-c的官方站点上有文章指引说明是怎样进行xml schema校验。

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

给出的样例代码:

// Instantiate the DOM parser.
XercesDOMParser parser;
parser.setDoNamespaces(true);
parser.setDoSchema(true);
parser.parse(xmlFile);

但。样例代码根本不起不论什么作用。

在调用XercesDOMParser::parse之前,还有两件事情要做:

1.调用XercesDOMParser::setValidationScheme来设置校验计划

parser.setValidationScheme( XercesDOMParser::Val_Auto);

parser.setValidationScheme( XercesDOMParser::Val_Always);

2.要调用XercesDOMParser::setErrorHandler, 当中參数必须是ErrorHandler类或子类的对象。


看以下样例

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> <House>good</House> <Street>Featherstone Street</Street> <Town>LONDON</Town> <PostCode>EC1Y 8SY</PostCode> <Country>UK</Country> </Address>


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 );
		parser.setDoNamespaces(true);
		parser.setDoSchema(true);
		//parser.setValidationScheme( XercesDOMParser::Val_Auto);
		parser.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;
}
能够看到这里的代码凝视掉了这一行:

//parser.setValidationScheme( XercesDOMParser::Val_Auto);


编译执行:

[[email protected] sample]$ g++ -g -o new_address new_address.cpp -lxerces-c
[[email protected] sample]$ ./new_address address.xml 
[[email protected] sample]$ 

//parser.setValidationScheme( XercesDOMParser::Val_Auto);
打开,但凝视掉

parser.setErrorHandler( &handler );
编译执行:

[[email protected] sample]$ g++ -g -o new_address new_address.cpp -lxerces-c
[[email protected] sample]$ ./new_address address.xml 
[[email protected] sample]$ 

parser.setErrorHandler( &handler );

打开,编译执行:

[[email protected] sample]$ ./new_address address.xml 
error in line:8, col:31, value 'EC1Y 8SY' does not match regular expression facet '[+\-]?[0-9]+'

执行一下xmllint,对照一下结果:

[[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>
	<House>good</House>
	<Street>Featherstone Street</Street>
	<Town>LONDON</Town>
	<PostCode>EC1Y 8SY</PostCode>
	<Country>UK</Country>
</Address>
address.xml:8: element PostCode: Schemas validity error : Element 'PostCode': 'EC1Y 8SY' is not a valid value of the atomic type 'xs:unsignedInt'.
address.xml fails to validate

PS:

在xml schema中,string是兼容其他类型,比方在House标签的内容写上数字,比方49,不管xmllint, 还是xerces都不会报这个标签的值有问题。

以前为这个问题折腾一个下午。

以上是关于用xerces-c来进行xml schema校验的主要内容,如果未能解决你的问题,请参考以下文章

XML Schema学习札记——基础总览

如何在xml中生成schema约束

Spring怎么查找xml和schema约束

xml约束技术---schema

在xml中xsd:schema和xs:schema是一个意思吗

JSON解析器之json schema校验及代码实现