2

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!

3 Answers 3

3

Remove the 2nd line from XML File

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
elementFormDefault="qualified" attributeFormDefault="unqualified">

The correct XML is

<?xml version="1.0" encoding="UTF-8"?>
<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>

Solution :

Please check the folder structure and run it again.

The Source Code :

package com.shashi.mpoole;


import java.io.File;

import javax.xml.XMLConstants;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

public class XMLValid {

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub

        if(validateAgainstXSD(new File("connector/connector.xml")))
        {
            System.out.println("Success");
        }
        else
        {
            System.out.println("Failure");
        }
    }



    static boolean validateAgainstXSD(File xml){
        try{

            SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            Schema schema = factory.newSchema(new StreamSource("connector/connector.xsd"));

            Validator validator  = schema.newValidator();
            validator.validate(new StreamSource(xml));
            return true;
        }catch(Exception exe){
            exe.printStackTrace();
        return false;
        }
    }

}

The Folder Structure

-----Project

    ------------- src

                  -------------XMLValid.java

    ------------- connector

                  -------------connector.xsd

                  -------------connector.xml 
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks a lot! It validates now using the website. But I am still getting the same SAXParseException in Java: 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>. Do you know what is causing this maybe? Again, thanks for your help!
Thanks once again, tried implementing it in my code. But I still can't get it to work. Got a MalformedURLException no protocol. So I've changed the file path to `file://connector/connector.xsd but I am still getting the same error.
Did you check the folder structure
Yes I placed all the files in the same structure as in your post, the exact exception I get is: java.net.MalformedURLException: no protocol: connector/connector.xsd
I did, I don't get it anymore >.<. I really don't know what I am doing wrong.
|
2

I was able to solve this type of problem by changing the XSD which had a namespace prefix of "xs:" and search/replacing all instances to be "xsd:" instead (and also the xmlns:xsd= definition). Then I hosted this XSD on an internal server, pointed there in my schemaLocation attribute in the XML, and everything worked.

I tried this because of the precisely worded error:

  • 3) the root element of the document is not <xsd:schema>.

I know, I know, this should not have worked, but it did. The external XSD which didn't work for me without translation is http://camel.apache.org/schema/spring/camel-spring.xsd

I'm not sure if this is a bug within SAX or Eclipse.

Comments

1

Try this code to validate your XSD, this one is the proper standard to avoid the exception what you get (this one is full proof code).

<?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="commandCode"  type="xs:string"/>
    <xs:element name="code"  type="xs:string"/>
    <xs:element name="value" type="xs:string" />
    <xs:element name="customerID" type="xs:string" />
    <xs:element name="MessageType" type="xs:string" />
    <xs:element name="resultCode" type="xs:string" />
    <xs:element name="transactionInfo" type="xs:string" />

<xs:element name="mark" >
    <xs:complexType>
        <xs:sequence>
            <xs:element ref="code" minOccurs="1" maxOccurs="1"/>
            <xs:element ref="value" minOccurs="1" maxOccurs="1"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

<xs:element name="marks" >
    <xs:complexType>
        <xs:sequence>
            <xs:element ref="mark" minOccurs="1" maxOccurs="unbounded" />
         <!--End element Mark -->
    </xs:sequence>
    </xs:complexType>
</xs:element> 
<xs:element name="commandLine">
    <xs:complexType>
        <xs:sequence minOccurs="1" maxOccurs="1">
            <xs:element ref="commandCode"  minOccurs="1" maxOccurs="1"/>
            <xs:element ref="marks" minOccurs="0" maxOccurs="1"/>
            <xs:element ref="customerID" minOccurs="0" maxOccurs="1"/>
            <xs:element ref="MessageType"  minOccurs="0" maxOccurs="1"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>
<xs:element name="statusInfo">
    <xs:complexType>
        <xs:sequence>
            <xs:element ref="transactionInfo" minOccurs="1" maxOccurs="1"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

<xs:element name="Antwoordregel" >
    <xs:complexType>
        <xs:sequence minOccurs="0" maxOccurs="1">
            <xs:element ref="resultCode" minOccurs="0" maxOccurs="1"/>
            <xs:element ref="statusInfo" minOccurs="0" maxOccurs="unbounded" />
        </xs:sequence>  
    </xs:complexType>
</xs:element>

<!-- First Content Document Element -->
<xs:element name="Content">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="commandLine" minOccurs="1" maxOccurs="1" />
                <xs:element ref="Antwoordregel" minOccurs="1" maxOccurs="1"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

</xs:schema>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.