Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Scout » XML validation with XSD
icon3.gif  XML validation with XSD [message #1235473] Fri, 24 January 2014 13:06 Go to next message
marco giudici is currently offline marco giudiciFriend
Messages: 204
Registered: February 2013
Location: Italy
Senior Member
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
Re: XML validation with XSD [message #1236524 is a reply to message #1235473] Mon, 27 January 2014 12:20 Go to previous messageGo to next message
Jeremie Bresson is currently offline Jeremie BressonFriend
Messages: 1252
Registered: October 2011
Senior Member
This answer on StackOverflow tells that the xmlns attribute of the XML file needs to match with the one defined in the XSD file. I did not verify it, but it sounds like you should try it.

This forum is about Eclipse Scout. I am happy to help you, but I think that for questions not directly related to Eclipse Scout (like XML - XSD + java), you will get better results on other forums or platforms like StackOverflow.
Re: XML validation with XSD [message #1236528 is a reply to message #1236524] Mon, 27 January 2014 12:37 Go to previous message
marco giudici is currently offline marco giudiciFriend
Messages: 204
Registered: February 2013
Location: Italy
Senior Member
Thank you for the suggestion.
Previous Topic:Problems with renamed server session
Next Topic:Problems layouting buttons on form
Goto Forum:
  


Current Time: Tue Mar 19 03:15:01 GMT 2024

Powered by FUDForum. Page generated in 0.02353 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top