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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:24:13+00:00 2026-05-24T13:24:13+00:00

I have some Java code that determines the namespace of the root-level element of

  • 0

I have some Java code that determines the namespace of the root-level element of an xml document using SAX. If the namespace is “http://sbgn.org/libsbgn/pd/0.1”, it should return version 1. If the namespace is “http://sbgn.org/libsbgn/0.2”, the version should be 2. So all the code does is read the first element, and set a variable based on the namespace. Here is the code:

private static class VersionHandler extends DefaultHandler
{
    private int version = -1;

    @Override
    public void startElement (String uri, String localName, String qName, Attributes attributes) throws SAXException
    {
        if ("sbgn".equals (qName))
        {
            System.out.println (uri);
            if ("http://sbgn.org/libsbgn/0.2".equals(uri))
            {
                version = 2;
            } 
            else if ("http://sbgn.org/libsbgn/pd/0.1".equals(uri))
            {
                version = 1;
            } 
            else
            {
                version = -1;
            }
        }
    }

    public int getVersion() { return version; }
};

public static int getVersion(File file) throws SAXException, FileNotFoundException, IOException
{
    XMLReader xr;   
    xr = XMLReaderFactory.createXMLReader();

    VersionHandler versionHandler = new VersionHandler();

    xr.setContentHandler(versionHandler);
    xr.setErrorHandler(versionHandler);
    xr.parse(new InputSource(
        InputStreamToReader.inputStreamToReader(
            new FileInputStream (file))));

    return versionHandler.getVersion();
}   

This works, but has two problems:

  1. It is inefficient, because the whole document will be parsed even though only the first element is needed.
  2. More importantly, this code sometimes (apparently depending on firewall configuration) triggers a UnknownHostException like so:
    java.net.UnknownHostException: www.w3.org 
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at sun.net.NetworkClient.doConnect(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.http.HttpClient.(Unknown Source)
    at sun.net.www.http.HttpClient.New(Unknown Source)
    at sun.net.www.http.HttpClient.New(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown
    Source)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown
    Source)
    at
    com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(Unknown
    Source)
    at
    com.sun.org.apache.xerces.internal.impl.XMLEntityManager.startEntity(Unknown
    Source)
    at
    com.sun.org.apache.xerces.internal.impl.XMLEntityManager.startDTDEntity(Unknown
    Source)
    at
    com.sun.org.apache.xerces.internal.impl.XMLDTDScannerImpl.setInputSource(Unknown
    Source)
    at
    com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$DTDDriver.dispatch(Unknown
    Source)
    at
    com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$DTDDriver.next(Unknown
    Source)
    at
    com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(Unknown
    Source)
    at
    com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown
    Source)
    at
    com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown
    Source)
    at
    com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown
    Source)
    at
    com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown
    Source)
    at
    com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown
    Source)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown
    Source)
    at
    com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown
    Source)
    at org.sbgn.SbgnVersionFinder.getVersion(SbgnVersionFinder.java:57)

So my questions are:

  1. Apparently this bit of code is connecting to the internet. How can I avoid that? Besides leading to problems with firewalls, it is also needlessly slow.
  2. Why is it connecting to the internet? Please help me understand the logic here, there should be absolutely no need for it.
  3. Is there a more efficient way to determine the namespace of the root element of an xml document?

Edit: here is a link to a sample document that I’m trying to parse this way: https://libsbgn.svn.sourceforge.net/svnroot/libsbgn/trunk/test-files/PD/adh.sbgn

Edit2: A note regarding to the solution of this bug: In fact the problem was triggered because the wrong document was being parsed, instead of the intended document, I was parsing an XHMTML document that does in fact refer to http://www.w3.org. Of course the solution is to use the correct document. Nevertheless, I found it useful to add this line:

 xr.setEntityResolver(null);

To prevent xerces from going over the internet when it’s really completely unnecessary.

  • 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-24T13:24:14+00:00Added an answer on May 24, 2026 at 1:24 pm

    I believe you need to set the entity resolver. See the javadoc. Also, this article seems relevant.

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

Sidebar

Related Questions

I have some java code that looks similar to this: private void startServer() throws
I'm using TPTP to profile some slow running Java code an I came across
I have a Java application that persists byte[] structures to a DB (using Hibernate).
Sometimes while writing Java in Eclipse, I write code that generates warnings. A common
If anyone familiar with Rebecca Wirfs-Brock, she has a piece of Java code found
I have an activity that implements LocationListener in my application and my onLocationChanged method
Several times in my career, I have worked in a software group that determined
I have encountered a strange Invalid Packet Lenght (that is how the error is
I'm very new to Java and am having some issues looping through JCheckBoxes on
Is there a good way to implement an execution policy that determines which Thread

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.