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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:22:51+00:00 2026-06-05T17:22:51+00:00

I am validating XML documents against a schema. Some more complex documents/schemas always fail

  • 0

I am validating XML documents against a schema. Some more complex documents/schemas always fail when trying to validate them using this code:

    DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
    dbfac.setNamespaceAware(true);
    dbfac.setIgnoringElementContentWhitespace(true);
    DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
    Document doc = docBuilder.parse("sampleResponse.xml");

    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Source schemaSource = new StreamSource(getClass().getResourceAsStream("/" + "SampleResponse.xsd"));

    Schema schema = schemaFactory.newSchema(schemaSource);
    Validator validator = schema.newValidator();
    Source source = new DOMSource(doc);
    // Set a custom error handler that simple re-throws every exception
    validator.setErrorHandler(new ValidationErrorHandler());
    validator.validate(source);

The problem is this line:

    Source schemaSource = new StreamSource(getClass().getResourceAsStream("/" + "SampleResponse.xsd"));

If I read the schema as a file, it works:

    Source schemaSource = new StreamSource(new File("somepath/SampleResponse.xsd"));

Why doesn’t validation work when I get the schema directly from classpath?

(Using Java 1.6 on Windows 7 64-bit)

Exception message when failing:
Could not validate against schema SampleResponse.xsd. Nested exception: src-resolve: Cannot resolve the name 'oa:Attachments' to a(n) 'element declaration' component.

  • 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-05T17:22:54+00:00Added an answer on June 5, 2026 at 5:22 pm

    When you pass a File to StreamSource, the InputStream is set to the contents of the file, but also the systemId is set to be the URL of the File. This allows relative URIs in your schema to be resolved. If your schema has any relative URLs, this is definitely your problem. To make those relative URLs resolvable when reading the schema from the classpath, you need to implement an EntityResolver. If you don’t use relative URIs there might still be other more subtle impacts of the systemId being null. I would recommend using the constructor

    StreamSource(InputStream inputStream, String systemId)
    

    Try setting systemId to: null, the File containing the schema, some other file, a File that doesn’t exist. That might give you a hint of what Validator is doing with the systemId.

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

Sidebar

Related Questions

I'm trying to develop a schema that will validate some existing XML files I've
In Java, I can validate an XML document against an XSD schema using javax.xml.validation.Validator,
I'm trying to validate my XML document against my XML schema. This is my
I'll admit, I'm an XML newbie. I'm having trouble validating some xml against a
I'm trying to read the data from an XML file, validating it against the
I need to validate a XML file against a schema. The XML file is
I'm trying to validade a XML against a W3C XML Schema. The following code
I'm trying to write a validation script that will validate XML against the NITF
I am validating an in-memory DOM object using the javax.xml.validation.Validator class against an XSD
I'm trying to put together some validation code. I am trying to validate against

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.