Org.w3c.dom.Document for a string without javax.xml.transform

I spent some time searching Google for a way to convert org.w3c.dom.Document into a string representation of the entire DOM tree, so I can save the object in the file system.

However, all the solutions you found use javax.xml.transform.Transformer, which is not supported as part of the Android 2.1 API. How to do this without using this class / containing package?

+4
source share
2 answers

To avoid using Transformer , you must manually iterate over the xml tree, otherwise you can rely on some external libraries. You should look here .

+3
source

Please try this code:

 DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); Document doc = docBuilder.parse("/path/to/file.xml"); DOMImplementation domImpl = ownerDocument.getImplementation(); DOMImplementationLS domImplLS = (DOMImplementationLS)domImpl.getFeature("LS", "3.0"); LSSerializer serializer = domImplLS.createLSSerializer(); serializer.getDomConfig().setParameter("xml-declaration", Boolean.valueOf(false)); LSOutput lsOutput = domImplLS.createLSOutput(); lsOutput.setCharacterStream(output); serializer.write(doc, lsOutput); 
+5
source

Source: https://habr.com/ru/post/1346083/


All Articles