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 6535927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:25:51+00:00 2026-05-25T10:25:51+00:00

I need to validate XML Schema Instance (XSD) documents which are programmatically generated so

  • 0

I need to validate XML Schema Instance (XSD) documents which are programmatically generated so I’m using the following Java snippet, which works fine:

SchemaFactory factory = SchemaFactory.newInstance(
    XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema xsdSchema = factory.newSchema( // Reads URL every time...
    new URL("http://www.w3.org/2001/XMLSchema.xsd"));
Validator xsdValidator = xsdSchema.newValidator();
xsdValidator.validate(new StreamSource(schemaInstanceStream));

However, when I save the XML Schema definition file locally and refer to it this way:

Schema schema = factory.newSchema(
    new File("test/xsd/XMLSchema.xsd"));

It fails with the following exception:

org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema document ‘file:/Users/foo/bar/test/xsd/XMLSchema.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’ve ensured that the file exists and is readable by doing exists() and canRead() assertions on the File object. I’ve also downloaded the file with a couple different utilities (web browser, wget) to ensure that there is no corruption.

Any idea why I can validate XSD instance documents when I generate the schema from the HTTP URL but I get the above exception when trying to generate from a local file with the same contents?

[Edit]

To elaborate, I’ve tried multiple forms of factory.newSchema(...) using Readers and InputStreams (instead of the File directly) and still get exactly the same error. Moreover, I’ve dumped the file contents before using it or the various input streams to ensure it’s the right one. Quite vexing.

Full Answer

It turns out that there are three additional files referenced by XML Schema which must be also stored locally and XMLSchema.xsd contains an import statement whose schemaLocation attribute must be changed. Here are the files that must be saved in the same directory:

  1. XMLSchema.xsd – change schemaLocation to “xml.xsd” in the “import” element for XML Namespace.
  2. XMLSchema.dtd – as is.
  3. datatypes.dtd – as is.
  4. xml.xsd – as is.

Thanks to @Blaise Doughan and @Tomasz Nurkiewicz for their hints.

  • 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-05-25T10:25:51+00:00Added an answer on May 25, 2026 at 10:25 am

    UPDATE

    Is XMLSchema.xsd importing any other schemas by relative paths that are not on the local file systen?


    Your relative path may not be correct wrt your working directory. Try entering a fully qualified path to eliminate the possibility that the file can not be found.

    org.xml.sax.SAXParseException: schema_reference.4: Failed to read
    schema document ‘file:/Users/foo/bar/test/xsd/XMLSchema.xsd’, because
    1) could not find the document; 2) the document could not be read; 3)
    the root element of the document is not .

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Java, I can validate an XML document against an XSD schema using javax.xml.validation.Validator,
I'm transforming XML to XML using the following XSLT. I need to validate the
I need to validate an XML agaist a schema. I tried XML::SAX::ParserFactory; XML::Validator::Schema and
Within a Silverlight library, I need to validate incoming XML against a schema. The
I need to validate a XML file against a schema. The XML file is
I have some Java code that validates XML against an XSD. I am using
I need to validate a small fragment of an xml file against a schema.
I need to validate a string that contains XML Data, there is no schema
I need to validate a xml file against DTD schema. I found out that
I need to create an XML schema that validates a tree structure of an

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.