java - 获取xml命名空间(不触发UnknownHostException)

标签 java xml sax xml-namespaces xerces

我有一些 Java 代码,它们使用 SAX 确定 xml 文档的根级元素的命名空间。如果命名空间为“http://sbgn.org/libsbgn/pd/0.1”,则应返回版本 1。如果命名空间为“http://sbgn.org/libsbgn/0.2”,则版本应为 2。因此,代码所做的就是读取第一个元素,并根据 namespace 设置一个变量。这是代码:

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();
}   

这可行,但有两个问题:

  1. 效率很低,因为即使只需要第一个元素,也会解析整个文档。
  2. 更重要的是,此代码有时(显然取决于防火墙配置)会触发 UnknownHostException,如下所示:
    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 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);

防止 xerces 在完全没有必要的情况下通过互联网进行传输。

最佳答案

我相信您需要设置实体解析器。请参阅javadoc 。另外,这个article似乎相关。

关于java - 获取xml命名空间(不触发UnknownHostException),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7017164/

相关文章:

Java:我比较了两个字符串,但它没有识别出来

php - 使用 Magento 找不到 Controller 404 页面

java - 在 API < & > 21 的 ListView 项上创建波纹效果

java - 使用 Java 读取 XML - SAX 解析器

java - 如何从大型 XML 中获取特定元素的值

java - 使用 SAX 解析器拆分 XML

Java swagger 与 JaxRS 抛出错误

java - 除非 javaFX 中两个事件为真,否则如何将按钮设置为保持禁用状态?

Java-8 lambda 表达式与函数式接口(interface)的行为

java - SAX - 无需 CDATA 即可读取 HTML 内容