|
#include <iostream>#include <list>#include <string>#include <xercesc/util/OutOfMemoryException.hpp>#include <xercesc/dom/DOM.hpp>#include <xercesc/parsers/XercesDOMParser.hpp>#include <xercesc/framework/XMLGrammarDescription.hpp>#include <xercesc/sax/ErrorHandler.hpp>#include <xercesc/sax/SAXParseException.hpp>using std::cerr;using std::endl;XERCES_CPP_NAMESPACE_USEclass StrX{char* fLocalForm;public :StrX(const XMLCh* const toTranscode) { fLocalForm = XMLString::transcode(toTranscode); }~StrX() { XMLString::release(&fLocalForm); }const char* localForm() const { return fLocalForm; }};std::ostream&operator<<(std::ostream& target, const StrX& toDump){target << toDump.localForm();return target;}class DOMTreeErrorReporter : public ErrorHandler{public:void warning(const SAXParseException& toCatch) {}void resetErrors() {}void DOMTreeErrorReporter::error(const SAXParseException& toCatch) {cerr << "Error at file /"" << StrX(toCatch.getSystemId())<< "/", line " << toCatch.getLineNumber()<< ", column " << toCatch.getColumnNumber() << endl<< " Message: " << StrX(toCatch.getMessage()) << endl;}void DOMTreeErrorReporter::fatalError(const SAXParseException& toCatch) {cerr << "Fatal Error at file /"" << StrX(toCatch.getSystemId())<< "/", line " << toCatch.getLineNumber()<< ", column " << toCatch.getColumnNumber() << endl<< " Message: " << StrX(toCatch.getMessage()) << endl;}};// ---------------------------------------------------------------------------intmain(int argc, const char** argv){try{XMLPlatformUtils::Initialize();}catch(const XMLException& e){StrX tmp_e(e.getMessage());cerr << "Xerces initialization error: " << tmp_e.localForm() << endl;return 2;}if ( argc < 1 ){cerr << "usage: schema-check <xml-file> [<schema-file> ...]" << endl;return 3;}XercesDOMParser *parser = new XercesDOMParser;DOMTreeErrorReporter *errReporter = new DOMTreeErrorReporter();parser->setErrorHandler(errReporter);parser->setDoNamespaces(true);parser->setCreateEntityReferenceNodes(true);parser->useCachedGrammarInParse(true);if ( argc > 2 ){parser->setDoSchema(true);parser->setDoValidation(true);parser->setValidationSchemaFullChecking(true);for ( int i = 2; i < argc; i++ ){if ( parser->loadGrammar(argv, Grammar::SchemaGrammarType, true) == 0 ){cerr << "Error loading grammar " << std::string(argv) << endl;return 4;}}}bool errorsOccured = true;try{parser->parse(argv[1]);errorsOccured = false;}catch ( const OutOfMemoryException& ){cerr << "Out of memory exception." << endl;}catch ( const XMLException& e ){cerr << "An error occurred during parsing" << endl<< " Message: " << StrX(e.getMessage()) << endl;}catch ( const DOMException& e ){const unsigned int maxChars = 2047;XMLCh errText[maxChars + 1];cerr << endl<< "DOM Error during parsing: '" << std::string(argv[1]) << "'" << endl<< "DOM Exception code is: " << e.code << endl;if ( DOMImplementation::loadDOMExceptionMsg(e.code, errText, maxChars) )cerr << "Message is: " << StrX(errText) << endl;}catch (...){cerr << "An error occurred during parsing." << endl;}if (!errorsOccured){cerr<<"验证成功"<<endl;return 1;}else{cerr<<"验证失败"<<endl;return 0;}}
-------------------------booklist.xsd----------------------------------------
<?xml version="1.0" encoding="GB2312"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="booklist">
<xs:complexType>
<xs:sequence>
<xs:element name="book" type="bookType" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="bookType">
<xs:sequence>
<xs:element name="ISBN" type="xs:string"/>
<xs:element name="title" type="xs:string"/>
<xs:element name="authorlist" type="authorlistType"/>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>
<xs:attribute name="classify" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="社会科学"/>
<xs:enumeration value="自然科学"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
<xs:complexType name="authorlistType">
<xs:sequence maxOccurs="4">
<xs:element name="author" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
-------------------------------booklist.xml--------------------------------
<?xml version="1.0" encoding="GB2312"?>
<booklist xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="booklist.xsd">
<book classify="自然科学">
<ISBN>7-302-12066-8</ISBN>
<title>JAVA使用教程</title>
<authorlist>
<author>Herbert Scihldt</author>
<author>马海军</author>
</authorlist>
<price>64.00</price>
</book>
<book classify="社会科学">
<ISBN>7-5037-1978</ISBN>
<title>投资学</title>
<authorlist>
<author>张中华</author>
<author>谢老三</author>
</authorlist>
<price>19.00</price>
</book>
</booklist>
使用如下命令行参数验证booklist.xml是否符合booklist.xsd
booklist.xml booklist.xsd
如果验证成功提示验证成功,否则出现异常 |
|
|