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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:31:16+00:00 2026-06-09T22:31:16+00:00

I have to write some code to handle reading and validating XML documents that

  • 0

I have to write some code to handle reading and validating XML documents that use a version attribute in their root element to declare a version number, like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> 
<Junk xmlns="urn:com:initech:tps" 
    xmlns:xsi="http://www3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="urn:com:initech.tps:schemas/foo/Junk.xsd"
    VersionAttribute="2.0">

There are a bunch of nested schemas, my code has an org.w3c.dom.ls.LsResourceResolver to figure out what schema to use, implementing this method:

LSInput resolveResource(String type,
                        String namespaceURI,
                        String publicId,
                        String systemId,
                        String baseURI)

Previous versions of the schema have embedded the schema version into the namespace, so I could use the namespaceURI and systemId to decide which schema to provide. Now the version number has been switched to an attribute in the root element, and my resolver doesn’t have access to that. How am I supposed to figure out the version of the XML document in the LsResourceResolver?

  • 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-09T22:31:17+00:00Added an answer on June 9, 2026 at 10:31 pm

    I had never had to deal with schema versions before this and had no idea what was involved. When the version was part of the namespace then I could throw all the schemas in together and let them get sorted out, but with the version in the root element and namespace shared across versions there is no getting around reading the version information from the XML before starting the SAX parsing.

    I’m going to do something very similar to what Pangea suggested (gets +1 from me), but I can’t follow the advice exactly because the document is too big to read it all into memory, even once. By using STAX I can minimize the amount of work done to get the version from the file. See this DeveloperWorks article, “Screen XML documents efficiently with StAX”:

    The screening or classification of XML documents is a common problem,
    especially in XML middleware. Routing XML documents to specific
    processors may require analysis of both the document type and the
    document content. The problem here is obtaining the required
    information from the document with the least possible overhead.
    Traditional parsers such as DOM or SAX are not well suited to this
    task. DOM, for example, parses the whole document and constructs a
    complete document tree in memory before it returns control to the
    client. Even DOM parsers that employ deferred node expansion, and thus
    are able to parse a document partially, have high resource demands
    because the document tree must be at least partially constructed in
    memory. This is simply not acceptable for screening purposes.

    The code to get the version information will look like:

    def map = [:]
    def startElementCount = 0
    def inputStream = new File(inputFile).newInputStream()
    try {
        XMLStreamReader reader = 
            XMLInputFactory.newInstance().createXMLStreamReader(inputStream)
        for (int event; (event = reader.next()) != XMLStreamConstants.END_DOCUMENT;) {
            if (event == XMLStreamConstants.START_ELEMENT) {
                if (startElementCount > 0) return map
                startElementCount += 1
                map.rootElementName = reader.localName
                for (int i = 0; i < reader.attributeCount; i++) {
                    if (reader.getAttributeName(i).toString() == 'VersionAttribute') {
                        map.versionIdentifier = reader.getAttributeValue(i).toString()
                        return map
                    }
                }
            }
        }   
    } finally {
        inputStream.close()
    }
    

    Then I can use the version information to figure out what resolver to use and what schema documents to set on the SaxFactory.

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

Sidebar

Related Questions

So far I have managed to write some code that should print the source
Question simple and quick: I have started to use Netbeans to write some code
I'd like to write a function that would have some optional code to be
I have some code that seems to not handle it well when a TCP
I have been trying to write some code in Scala to read a file
I'm trying to write some C# code to do administration on IIS. I have
I have a couple of web-pages where I write out some code c# ,
I currently using android NDK to write some native code in C. I have
I have some C++ code that is writing to a socket that is being
I need to write some code that registers an event handler for the SiteMapResolve

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.