Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9173207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:32:08+00:00 2026-06-17T16:32:08+00:00

I have an XML file and a XSD file, I want to validate the

  • 0

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>test@test.com</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: http://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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:32:10+00:00Added an answer on June 17, 2026 at 4:32 pm

    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>test@test.com</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 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to validate an XML file against an XSD schema. The XML files
I want to validate xml against xsd file. So I would like to install
I want to write an XML file. I have created an XSD file named
I have a Xml-Schema file to validate xml-Files. Xml-Schema: <?xml version=1.0 encoding=UTF-8?> <xs:schema xmlns:xs=http://www.w3.org/2001/XMLSchema
I have an XSD file and I want to create an XML file at
I have some XML file i want to validate and i have to do
I want to define an XSD to an XML file, but I don't know
I have an XML file that I used to create an XSD File and
I have an XML Schema Definition file .XSD who's elements are in Spanish. I
I have been provided an XSD and a sample Xml file that contains the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.