org.apache.xerces.dom.DeferredDocumentImpl 与 org.dom4j.Document 不兼容
我从 URL 读取一些 RSS,但遇到了一些麻烦。 最初我有一个像这样的简单实现:
SAXReader reader = new SAXReader();
Document doc = reader.read(new URL(sURL));
但是,如果响应非常慢,这不允许我使请求超时。所以我将其更改为:
public static org.dom4j.Document readXml(InputStream is) throws SAXException, IOException,
ParserConfigurationException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(false);
dbf.setIgnoringComments(false);
dbf.setIgnoringElementContentWhitespace(true);
dbf.setNamespaceAware(true);
DocumentBuilder db = null;
db = dbf.newDocumentBuilder();
return (org.dom4j.Document)db.parse(is);
}
SAXReader reader = new SAXReader();
URL myUrl = new URL(sURL);
URLConnection c = myUrl.openConnection();
c.setConnectTimeout(10000);
c.setReadTimeout(10000);
org.dom4j.Document doc = readXml(c.getInputStream());
Element root = doc.getRootElement();
尝试此操作时,我收到一个令人烦恼的错误:
org.apache.xerces.dom.DeferredDocumentImpl incompatible with org.dom4j.Document
如何避免这种情况?上述方法都不应该返回该类型的文档,我也尝试转换为正确的文档类型。
编辑:问题是 db.parse(is) 返回 org.w3c.dom ..
Im reading some RSS from an URL and are experiencing some troubles.
Initially I had a straightforward implementation like this:
SAXReader reader = new SAXReader();
Document doc = reader.read(new URL(sURL));
However, this didnt allow me to timeout the request if the response was very slow. So I changed it to :
public static org.dom4j.Document readXml(InputStream is) throws SAXException, IOException,
ParserConfigurationException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(false);
dbf.setIgnoringComments(false);
dbf.setIgnoringElementContentWhitespace(true);
dbf.setNamespaceAware(true);
DocumentBuilder db = null;
db = dbf.newDocumentBuilder();
return (org.dom4j.Document)db.parse(is);
}
SAXReader reader = new SAXReader();
URL myUrl = new URL(sURL);
URLConnection c = myUrl.openConnection();
c.setConnectTimeout(10000);
c.setReadTimeout(10000);
org.dom4j.Document doc = readXml(c.getInputStream());
Element root = doc.getRootElement();
When trying this, I get a annyoing error:
org.apache.xerces.dom.DeferredDocumentImpl incompatible with org.dom4j.Document
How can I avoid this? None of the above methods are supposed to return that type of Document, and I also try to cast to the correct document type..
EDIT: The problem is db.parse(is) which returns org.w3c.dom ..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
确保您的
Element
类型为org.dom4j.Element
,而不是org.w3c.dom
、javax.bind.xml< /code> 等。
换句话说,dom4j API 与 Java 内置的 XML API 不兼容。两者根本不会混合在一起,除非您使用字符串进行操作(例如,在 dom4j 中生成 XML 并使用 Java 的 XML 解析它,反之亦然)。
Make sure your
Element
is of typeorg.dom4j.Element
and notorg.w3c.dom
,javax.bind.xml
, etc.In other words, dom4j API is not compatible with Java's built-in XML API. The two simply do not mix together, unless you operate with Strings (e.g. generate XML in dom4j and parse it with Java's XML or vice versa).
问题解决了!
通过使用:
从
org.w3c.dom.Document
创建org.dom4j.Document
Problem solved!
By using :
To create an
org.dom4j.Document
from theorg.w3c.dom.Document