2009-10-12 79 views

回答

1

xerces具有Java,C++和Perl版本。

爲方便起見,perl版本包含命令行驗證程序。

Java版本API包括用於validation

實施例的代碼的類和示例代碼:

// parse an XML document into a DOM tree 
DocumentBuilderFactory parserFactory = DocumentBuilderFactory.newInstance(); 
parserFactory.setNamespaceAware(true); 
DocumentBuilder parser = parserFactory.newDocumentBuilder(); 
Document document = parser.parse(new File("instance.xml")); 

// create a SchemaFactory capable of understanding WXS schemas 
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 

// load a WXS schema, represented by a Schema instance 
Source schemaFile = new StreamSource(new File("mySchema.xsd")); 
Schema schema = factory.newSchema(schemaFile); 

// create a Validator instance, which can be used to validate an instance document 
Validator validator = schema.newValidator(); 

// validate the DOM tree 
try { 
    validator.validate(new DOMSource(document)); 
} catch (SAXException e) { 
    // instance document is invalid! 
} 
+0

xerces看起來很酷。但不是使用JNI? – 2009-10-12 20:25:41

+0

nope,純java – skaffman 2009-10-12 20:29:09