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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:29:48+00:00 2026-05-29T06:29:48+00:00

How can one validate an XML file using an XSD in Java? We don’t

  • 0

How can one validate an XML file using an XSD in Java? We don’t know the schema in advance. I would like to be able to get the schemaLocation, download the XSD, cache it and then perform the actual validation.

The problem is, that with javax.xml.parsers.DocumentBuilder/DocumentBuilderFactory classes I can’t seem to be able to get a hold of the schemaLocation in advance. What’s the trick for this? Which classes should I look into?

Perhaps there’s a more suitable API I can use? The whole problem is that we need to validate dynamically, without (necessarily) having the XSDs locally.

How could one get a hold of the URL of schemaLocation defined in the XSD file?

I know you can set features/attributes, but that’s a different thing. I need to get the schemaLocation from the XSD first.

Please advise!

  • 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-29T06:29:49+00:00Added an answer on May 29, 2026 at 6:29 am

    Given that you are using Xerces (or JDK default), have you tried setting this feature to true on the factory: http://apache.org/xml/features/validation/schema. There are other features that you can play with regarding schemas: http://xerces.apache.org/xerces2-j/features.html

    UPDATE 2 (for caching):

    Implement a org.w3c.dom.ls.LSResourceResolver and set this on the SchemaFactory using the setResourceResolver method. This resolver would either get the schema from cache or fetch it from wherever the location refers to.

    UPDATE 3:

    LSResourceresolver example (which I think will be a good starting point for you):

    /**
     * Resolves resources from a base URL
     */
    public class URLBasedResourceResolver implements LSResourceResolver {
    
    private static final Logger log = LoggerFactory
            .getLogger(URLBasedResourceResolver.class);
    
    private final URI base;
    
    private final Map<URI, String> nsmap;
    
    public URLBasedResourceResolver(URL base, Map<URI, String> nsmap)
            throws URISyntaxException {
        super();
        this.base = base.toURI();
        this.nsmap = nsmap;
    }
    
    @Override
    public LSInput resolveResource(String type, String namespaceURI,
            String publicId, String systemId, String baseURI) {
        if (log.isDebugEnabled()) {
            String msg = String
                    .format("Resolve: type=%s, ns=%s, publicId=%s, systemId=%s, baseUri=%s.",
                            type, namespaceURI, publicId, systemId, baseURI);
            log.debug(msg);
        }
        if (type.equals(XMLConstants.W3C_XML_SCHEMA_NS_URI)) {
            if (namespaceURI != null) {
                try {
                    URI ns = new URI(namespaceURI);
                    if (nsmap.containsKey(ns))
                        return new MyLSInput(base.resolve(nsmap.get(ns)));
                } catch (URISyntaxException e) {
                    // ok
                }
            }
        }
        return null;
    }
    
    }
    

    The implementation of MyLSInput is really boring:

    class MyLSInput implements LSInput {
    
    private final URI url;
    
    public MyLSInput(URI url) {
        super();
        this.url = url;
    }
    
    @Override
    public Reader getCharacterStream() {
        return null;
    }
    
    @Override
    public void setCharacterStream(Reader characterStream) {
    
    }
    
    @Override
    public InputStream getByteStream() {
        return null;
    }
    
    @Override
    public void setByteStream(InputStream byteStream) {
    
    }
    
    @Override
    public String getStringData() {
        return null;
    }
    
    @Override
    public void setStringData(String stringData) {
    
    }
    
    @Override
    public String getSystemId() {
        return url.toASCIIString();
    }
    
    @Override
    public void setSystemId(String systemId) {
    }
    
    @Override
    public String getPublicId() {
        return null;
    }
    
    @Override
    public void setPublicId(String publicId) {
    }
    
    @Override
    public String getBaseURI() {
        return null;
    }
    
    @Override
    public void setBaseURI(String baseURI) {
    
    }
    
    @Override
    public String getEncoding() {
        return null;
    }
    
    @Override
    public void setEncoding(String encoding) {
    
    }
    
    @Override
    public boolean getCertifiedText() {
        return false;
    }
    
    @Override
    public void setCertifiedText(boolean certifiedText) {
    
    }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to validate an XML file against an XSD using the function schemaValidate(String
I have one xsd file ,I used xmlpad to validate this xsd against xml
Can one specify XML attribute values as CDATA ? If yes - what would
I got a simple XML file that I want to validate against an XSD.
Is it possible using one or more XSDs to validate the following xml structure
I'd like to be able to read in an XML schema (i.e. xsd) and
I was wondering how one would go about using sqlite3 to validate users in
How can one load custom (not an image, nor a sound file) resource file
Within a Silverlight library, I need to validate incoming XML against a schema. The
I have two xml files and I need to create one xsd for both.

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.