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 File
<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>
Code that validates XML against 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;
}
It throws
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)
Update: After Davis's answer
[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.
The target namespace for your schema is http://xml.netbeans.org/schema/Person , but the sample xml isn't in a namespace at all. Probably you want something like:
<p:root xmlns:p="http://xml.netbeans.org/schema/Person">
...
</p:root>
As David says, but you have to add the namespace to all tags.
Either
<p:root xmlns:p="http://xml.netbeans.org/schema/Person">
<p:persons>
<p:person>
...
</p:person>
<p:persons>
</p:root>
Or
<root xmlns="http://xml.netbeans.org/schema/Person">
<persons>
...
</persons>
</root>
David's answer is correct about the namespace qualification, your XML should look like:
<root xmlns="http://xml.netbeans.org/schema/Person">
<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>
Even with a correct XML schema, I have seen that error if the parser can not look up the XML schema correctly. The method you are using is quite old. If you are using Java SE 5 or later, you can use the following code to perform validation while parsing an XML document:
import java.io.File;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.validation.*;
public class Demo {
public static void main(String[] args) throws Exception {
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(new File("person.xsd"));
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
dbf.setSchema(schema);
DocumentBuilder db = dbf.newDocumentBuilder();
db.parse(new File("person.xml"));
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.