I have an XML file and a XSD file, I want to validate the XML against the XSD.
But I keep getting the following error:
org.xml.sax.SAXParseException; schema_reference.4: Failed to read schema document '/connector/connector.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.
I printed the canonical path to make sure I was trying to use to right file. But it wont work.
The XML:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<Content>
<commandLine>
<commandCode>A1</commandCode>
<marks><mark><code>mail</code><value>[email protected]</value></mark></marks>
<customerID>1</customerID>
<MessageType>2</MessageType>
</commandLine>
<Antwoordregel></Antwoordregel>
</Content>
</xs:schema>
XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema id="Message" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="Content">
<xs:complexType>
<xs:sequence>
<xs:element name="commandLine" minOccurs="1" maxOccurs="1">
<xs:complexType>
<xs:sequence minOccurs="1" maxOccurs="1">
<xs:element name="commandCode" type="xs:string" minOccurs="1" maxOccurs="1"/>
<xs:element name="marks" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence >
<xs:element name="mark" minOccurs="1" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence >
<xs:element name="code" type="xs:string" minOccurs="1" maxOccurs="1"/>
<xs:element name="value" type="xs:string" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="customerID" type="xs:string" minOccurs="0" maxOccurs="1"/>
<xs:element name="MessageType" type="xs:string" minOccurs="0" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Antwoordregel" minOccurs="1" maxOccurs="1">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="1">
<xs:element name="resultCode" type="xs:string" minOccurs="0" maxOccurs="1"/>
<xs:element name="statusInfo" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="transactionInfo" type="xs:string" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
The code I am using to validate:
static boolean validateAgainstXSD(String xml){
try{
File xsd = new File("connector/connector.xsd");
System.out.println(xsd.getCanonicalPath());
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(new StreamSource("/connector/connector.xsd"));
System.out.println(schema.toString());
Validator validator = schema.newValidator();
validator.validate(new StreamSource(xml));
return true;
}catch(Exception exe){
exe.printStackTrace();
return false;
}
}
It will always return false. I tried to validate the XML with the XSD with an online tool, which can be found here: www.utilities-online.info/xsdvalidation. This validator returns:
Not valid.
Error - Line 2, 122: org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 122; cvc-elt.1: Cannot find the declaration of element 'xs:schema'.
What can I do to solve this issue?
Any help is appreciated,
Thanks!