XSD
<?xml version="1.0" encoding="windows-1252"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://xml.netbeans.org/schema/Person"
xmlns:tns="http://xml.netbeans.org/schema/Person"
elementFormDefault="qualified">
<xsd:element name="root">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="persons">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="person" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="name" type="xsd:string"></xsd:element>
<xsd:element name="age" type="xsd:int"></xsd:element>
<xsd:element name="address" type="xsd:string"></xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
XML-файл
<root>
<persons>
<person>
<name>name1</name>
<age>1</age>
<address>abc abc abc1</address>
</person>
<person>
<name>name2</name>
<age>2</age>
<address>abc abc abc2</address>
</person>
<person>
<name>name2</name>
<age>3</age>
<address>abc abc abc3</address>
</person>
</persons>
</root>
Код, который проверяет XML на соответствие XSD:
public static String validateXMLwithXSD(String xmlFile, String xsdFile) {
String result = "SUCCESS";
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
System.out.println("DocumentBuilderFactory: " + factory.getClass().getName());
factory.setNamespaceAware(true);
factory.setValidating(true);
factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");
// Specify our own schema - this overrides the schemaLocation in the xml file
factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", "d:\\person.xsd");
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setErrorHandler(new SimpleErrorHandler());
Document document = builder.parse(xmlFile);
Node rootNode = document.getFirstChild();
System.out.println("Root node: " + rootNode.getNodeName());
} catch (Exception ex) {
System.out.println("--------------------");
result = "FAILURE:" + ex.getMessage();
ex.printStackTrace();
}catch (Error ex) {
System.out.println("--------------------");
result = "FAILURE:" + ex.getMessage();
ex.printStackTrace();
}
return result;
}
бросает
DocumentBuilderFactory: com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl
org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'root'.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
Обновление:
После ответа Дэвиса
[exec:exec]
DocumentBuilderFactory: com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl
org.xml.sax.SAXParseException: cvc-complex-type.2.4.a: Invalid content was found starting with element 'persons'. One of '{"http://xml.netbeans.org/schema/Person":persons}' is expected.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
error: cvc-complex-type.2.4.a: Invalid content was found starting with element 'persons'. One of '{"http://xml.netbeans.org/schema/Person":persons}' is expected.