Search code examples
javadocumentxerces

org.apache.xerces.dom.DeferredDocumentImpl incompatible with org.dom4j.Document


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 ..


Solution

  • 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