java org.apache.xerces.dom.DeferredDocumentImpl 与 org.dom4j.Document 不兼容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5456485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
org.apache.xerces.dom.DeferredDocumentImpl incompatible with org.dom4j.Document
提问by matskn
Im reading some RSS from an URL and are experiencing some troubles. Initially I had a straightforward implementation like this:
我从 URL 中读取了一些 RSS,但遇到了一些麻烦。最初我有一个简单的实现,如下所示:
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:
尝试此操作时,我收到一个 annyoing 错误:
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 ..
编辑:问题是 db.parse(is) 返回 org.w3c.dom ..
回答by mindas
Make sure your Element
is of type org.dom4j.Element
and not org.w3c.dom
, javax.bind.xml
, etc.
确保你的Element
类型是org.dom4j.Element
不org.w3c.dom
,javax.bind.xml
等等。
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).
换句话说,dom4j API 与 Java 的内置 XML API 不兼容。这两者根本不会混合在一起,除非您使用字符串进行操作(例如,在 dom4j 中生成 XML 并使用 Java 的 XML 解析它,反之亦然)。
回答by matskn
Problem solved!
问题解决了!
By using :
通过使用 :
DOMReader domReader = new DOMReader();
org.dom4j.Document dom4jDoc = domReader.read(doc);
org.dom4j.Element root = (Element)dom4jDoc.getRootElement();
To create an org.dom4j.Document
from the org.w3c.dom.Document
要创建一个org.dom4j.Document
从org.w3c.dom.Document