Hi all, Following is description of an XML document validation, with custom java code, that I've tried,
1) My XML and XSD documents x1.xml <?xml version="1.0"?> <m id="1"> <p> <x>hello 1</x> <y>there 1</y> </p> <p> <x>hello 2</x> <y>there 2</y> </p> <p> <x>hello 3</x> <y>there 3</y> </p> </m> x1.xsd <?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="p"> <xs:complexType> <xs:sequence> <xs:element name="x" type="xs:string"/> <xs:element name="y" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> 2) Requirement description I wish to validate, each XML instance "p" element (within the document x1.xml), without caring what is outside XML instance element "p". To do this, initially I construct an object of type org.w3c.dom.Document using a Xerces java DOM parser. I then traverse the Document object and validate each "p" element node with a schema (x1.xsd). For the above mentioned example, the validation would be invoked thrice (since there are three "p" XML elements in the instance document). 3) Following is a java program, for the above stated requirements, import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.dom.DOMSource; import javax.xml.validation.Schema; import javax.xml.validation.SchemaFactory; import javax.xml.validation.Validator; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.ErrorHandler; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; public class XSDValidationSample { public static void main(String[] args) { try { SchemaFactory schemaFactory = SchemaFactory.newInstance(" http://www.w3.org/XML/XMLSchema/v1.1"); Schema schema = schemaFactory.newSchema(new URL("file:///c:/<path>/x1.xsd")); Validator validator = schema.newValidator(); validator.setErrorHandler(new XSValidatorErrorHandler()); DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); docBuilderFactory.setNamespaceAware(true); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); Document document = docBuilder.parse("file:///c:/<path>/x1.xml"); NodeList nodeList = document.getElementsByTagName("p"); for (int idx = 0; idx < nodeList.getLength(); idx++) { Node node = nodeList.item(idx); validator.validate(new DOMSource(node)); // this invokes validation for each XML instance "p" element node } } catch (SAXException ex) { ex.printStackTrace(); } catch (MalformedURLException ex) { ex.printStackTrace(); } catch (ParserConfigurationException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } } static class XSValidatorErrorHandler implements ErrorHandler { @Override public void error(SAXParseException spe) throws SAXException { int lineNo = spe.getLineNumber(); int colNo = spe.getColumnNumber(); String errorMesg = spe.getMessage(); System.err.println("[Error] : (" + lineNo + ", " + colNo + ") " + errorMesg); } @Override public void fatalError(SAXParseException spe) throws SAXException { int lineNo = spe.getLineNumber(); int colNo = spe.getColumnNumber(); String errorMesg = spe.getMessage(); System.err.println("[Fatal Error] : (" + lineNo + ", " + colNo + ") " + errorMesg); } @Override public void warning(SAXParseException spe) throws SAXException { int lineNo = spe.getLineNumber(); int colNo = spe.getColumnNumber(); String errorMesg = spe.getMessage(); System.err.println("[Warning] : (" + lineNo + ", " + colNo + ") " + errorMesg); } } } Though, the above programs works fine. But any validation errors that are reported (in case, if there are any within XML instance document that's validated), do not show line and column numbers of error locations (with the above program, error locations are displayed as -1). Is it somehow possible with Xerces, to display XML instance document error location line and column numbers, when using DOMSource as an argument to javax.xml.validation.Validator.validate(..) call? -- Regards, Mukul Gandhi