How to convert a org.w3c.dom.Document object to a String in java?

How to convert a org.w3c.dom.Document object to a String in java?

You can convert an org.w3c.dom.Document object to a string in Java by serializing it using XML serialization. Here's a step-by-step guide on how to do this:

import java.io.StringWriter; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.dom.DOMSource; import org.w3c.dom.Document; public class DocumentToStringExample { public static void main(String[] args) { try { // Create a sample XML Document Document document = createSampleDocument(); // Convert the Document to a String String xmlString = documentToString(document); // Print the XML String System.out.println(xmlString); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } } public static Document createSampleDocument() { // Create a sample XML Document (for demonstration purposes) // You can replace this with your actual Document creation logic // Here, we create a simple XML Document with a root element and a child element // <root> // <child>Hello, World!</child> // </root> // ... Create and return the Document ... } public static String documentToString(Document document) throws TransformerConfigurationException, TransformerException { // Create a TransformerFactory TransformerFactory transformerFactory = TransformerFactory.newInstance(); // Create a StringWriter to store the XML content as a string StringWriter stringWriter = new StringWriter(); // Create a Transformer to perform the serialization Transformer transformer = transformerFactory.newTransformer(); // Serialize the Document into the StringWriter transformer.transform(new DOMSource(document), new StreamResult(stringWriter)); // Get the XML content as a string String xmlString = stringWriter.toString(); return xmlString; } } 

In this example:

  1. We create a sample XML Document using the createSampleDocument() method (you should replace this with your actual Document creation logic).

  2. We define a documentToString() method that takes a Document as input and converts it to a string.

  3. Inside the documentToString() method, we use the Java XML Transformation (JAXP) API to perform the conversion. We create a TransformerFactory, a StringWriter to store the serialized XML content, and a Transformer to perform the serialization.

  4. We use the transform() method to serialize the Document into the StringWriter.

  5. Finally, we retrieve the XML content as a string using stringWriter.toString() and return it.

When you run this code, it will print the XML content of the Document as a string.


More Tags

android-collapsingtoolbarlayout puppeteer android-permissions integer-overflow linux-device-driver centos onchange asp.net-core-2.1 pycharm post-increment

More Java Questions

More Entertainment Anecdotes Calculators

More Mortgage and Real Estate Calculators

More Dog Calculators

More General chemistry Calculators