我有一些 Java 代码,它们使用 SAX 确定 xml 文档的根级元素的命名空间。如果命名空间为“http://sbgn.org/libsbgn/pd/0.1”,则应返回版本 1。如果命名空间为“http://sbgn.org/libsbgn/0.2”,则版本应为 2。因此,代码所做的就是读取第一个元素,并根据名称空间设置一个变量。这是代码:
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();
}
这可行,但有两个问题:
- 效率低下,因为即使只需要第一个元素,也会解析整个文档。
- 更重要的是,这段代码有时(显然取决于防火墙配置)会触发 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)
所以我的问题是:
- 显然这段代码正在连接到互联网。我怎样才能避免这种情况?除了导致防火墙出现问题之外,它还造成不必要的缓慢。
- 为什么它要连接到互联网?请帮我理解一下这里的逻辑,应该完全没有必要。
- 有没有更有效的方法来确定 xml 文档根元素的命名空间?
编辑:这里是我尝试以这种方式解析的示例文档的链接: https://libsbgn.svn.sourceforge.net/svnroot/libsbgn/trunk/test-files/PD/adh.sbgn
Edit2:关于此错误解决方案的说明:事实上,触发问题的原因是解析了错误的文档,而不是预期的文档,我正在解析实际上引用 www.w3.org 的 XHMTML 文档。当然解决方案是使用正确的文档。尽管如此,我发现添加这一行很有用:
xr.setEntityResolver(null);
To Prevent xerces from over the internet when it is确实完全没有必要。
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:
- It is inefficient, because the whole document will be parsed even though only the first element is needed.
- 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:
- 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.
- Why is it connecting to the internet? Please help me understand the logic here, there should be absolutely no need for it.
- 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);
To prevent xerces from going over the internet when it's really completely unnecessary.
发布评论
评论(2)
我相信您需要设置实体解析器。请参阅 javadoc。另外,这篇文章似乎相关。
I believe you need to set the entity resolver. See the javadoc. Also, this article seems relevant.
它可能正在连接到 Internet,因为您的文档引用了 DTD 或 W3C 网站上的其他外部实体。今年早些时候,W3C 停止提供这些文档,因为他们无法处理流量。
一旦您查看了所需的文档内容,就可以通过从回调之一抛出 SAXException 来解决读取整个文档的问题。确保在调用 XMLReader.parse() 方法的代码中将此异常与解析器本身引发的异常区分开来(例如,您可以子类化 SAXException:尽管并非所有解析器都会原样抛出原始异常,并且您可能需要进行试验。 )
It's probably connecting to the internet because your document is referring to a DTD or other external entity on the W3C web site. Earlier this year, W3C stopped serving these documents because they couldn't handle the traffic.
You can solve the problem of reading the whole document by throwing a SAXException from one of your callbacks once you've seen as much of the document as you need to see. Be sure in the code that calls the XMLReader.parse() method to distinguish this exception from exceptions thrown by the parser itself (for example, you could subclass SAXException: though not all parsers throw your original exception unchanged and you may need to experiment.)