javaxpathdom4j

Converting org.w3c.dom.Element to org.dom4j.Element


I want to use javax.xml.xpath XPath APIs for my XML Reading and writing.

My whole Application uses dom4j elements and now javax.xml.xpath API is not accepting org.dom4j.document. So I have converted those documents to org.w3c.dom.Document using the below code

org.w3c.dom.Document w3cDom = new DOMWriter().write(doc);

I have used for API's like evaluate and I have got node of type org.w3c.dom

Since my other code is of completely dom4j related, Is there any way where I can convert org.w3c.dom.(Node or Element) to org.dom4j.(Node or Element) ??

PS:- I don't want to use dom4j.XPath as it internally calls jaxen.jar


Solution

  • Use the dom4j DOMReader.read() method:

    org.w3c.dom.Document w3cDom = new DOMWriter().write(doc);
    org.dom4j.io.DOMReader reader = new DOMReader();
    org.dom4j.Document document = reader.read(w3cDoc);
    

    Or use the DOMReader.readElement() method.