XML validation with XSD [message #1235473] |
Fri, 24 January 2014 13:06 |
|
Hi all,
I try to validate one xml file with a specific xsd schema, to do this I use "javax.xml.validation.Validator" and this is my source code:
try {
//Validate using internal XSD
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(new StreamSource(xsd));
Validator validator = schema.newValidator();
InputStream xml = new ByteArrayInputStream(xmlOrderStr.getBytes("UTF-8"));
validator.validate(new StreamSource(xml));
result = true;
}
catch (SAXException e) {
System.out.println(e.getMessage());
errorMsg = e.getMessage();
e.printStackTrace();
result = false;
}
catch (IOException e) {
System.out.println(e.getMessage());
errorMsg = e.getMessage();
e.printStackTrace();
result = false;
}
Variable xsd contains my XSD schema file
Variable xml contains my XML file
When this source code is executed the following error message is reported: "cvc-elt.1: Cannot find the declaration of element xxx". This error appears for the root element of my xml file.
Just to clarify the situation I use a simple example of xml and xsd file found on the net, but give the same error message on the element "<Employee>":
Employee.xsd
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.journaldev.com/Employee"
xmlns:empns="http://www.journaldev.com/Employee" elementFormDefault="qualified">
<element name="empRequest" type="empns:empRequest"></element>
<element name="empResponse" type="empns:empResponse"></element>
<complexType name="empRequest">
<sequence>
<element name="id" type="int"></element>
</sequence>
</complexType>
<complexType name="empResponse">
<sequence>
<element name="id" type="int"></element>
<element name="role" type="string"></element>
<element name="fullName" type="string"></element>
</sequence>
</complexType>
</schema>
Employee.xml
<?xml version="1.0"?>
<Employee>
<name>Pankaj</name>
<age>29</age>
<role>Java Developer</role>
<gender>Male</gender>
</Employee>
Can anyone help me understand why this happens?
Thanks in advance for any help or explanation
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03151 seconds