org.w3c.dom.Document to String without javax.xml.transform

18,152

Solution 1

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

Solution 2

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);
Share:
18,152
Kurru
Author by

Kurru

Updated on June 20, 2022

Comments

  • Kurru
    Kurru almost 2 years

    I've spent a while looking around on Google for a way to convert a org.w3c.dom.Document to a string representation of the whole DOM tree, so I can save the object to file system.

    However all the solutions I've found use javax.xml.transform.Transformer which isn't supported as part of the Android 2.1 API. How can I do this without using this class/containing package?

  • JasonPlutext
    JasonPlutext about 11 years
    Nice, but not supported by org.w3c.tidy.DOMDocumentImpl for example.
  • JasonPlutext
    JasonPlutext about 11 years
    JTidy doesn't implement LSSerializer