2

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

Nipuna Priyamal
  • 370
  • 2
  • 14
Mahipal Reddy
  • 389
  • 6
  • 20

1 Answers1

0
        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());
Mahipal Reddy
  • 389
  • 6
  • 20