How to covert "org.w3c.dom.Document" object to "org.apache.commons.jxpath.xml.DocumentContainer" object in Java

66 Views Asked by At

I have a document object and need to parse XML using JXPath (which needs DocumentContainer object). Is there any to way to create DocumentContainer from Document.

I don't have the file available physically

1

There are 1 best solutions below

0
Mahipal Reddy On
        Document document = .....;
        //Creating a temp file
        File tempXMLFile = File.createTempFile("file_name", ".xml");

        //Writing document content into temp file
        DOMSource source = new DOMSource(document);
        FileWriter writer = new FileWriter(tempXMLFile);
        StreamResult result = new StreamResult(writer);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.transform(source, result);

        //Creating DocumentContainer object to parse using JXPath
        DocumentContainer documentContainer = new DocumentContainer(tempXMLFile.toURI().toURL());