<Retail> <product> </product> <product> </product> <product> </product> </Retail>
package com.xmlpack; import java.io.File; import java.io.IOException; import javax.xml.XMLConstants; import javax.xml.transform.Result; import javax.xml.transform.Source; import javax.xml.transform.stream.StreamSource; import javax.xml.validation.*; import org.xml.sax.SAXException; public class SampleXmlValidator { public static void main(String args[]) throws SAXException, IOException { // Construct a StreamSource from a File. Source xmlFile = new StreamSource(new File( "D:/SampleXML/sample/src/com/xml/product.xml")); // SchemaFactory is a schema compiler. It reads external representations // of schemas and prepares them for validation. SchemaFactory schemaFactory = SchemaFactory .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); // This object represents a set of constraints that can be checked/ // enforced against an XML document. Schema schema = schemaFactory.newSchema(new File( "D:/SampleXML/sample/src/com/xsd/product.xsd")); // A processor that checks an XML document against Schema. Validator validator = schema.newValidator(); try { validator.validate(xmlFile); System.out.println(xmlFile.getSystemId() + " is valid"); } catch (SAXException e) { System.out.println(xmlFile.getSystemId() + " is NOT valid"); System.out.println("Reason: " + e.getLocalizedMessage()); } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)