Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:


1. Introduction

XML (eXtensible Markup Language) is one of the most popular schemas for structuring information. Moreover, the parsing and manipulation of XML Documents in Java are commonly accomplished using technologies like DOM (Document Object Model) and SAX (Simple API for XML).

In some cases, it might be necessary to transform an XML Document object into its string form, which could be used to store the XML information inside a database or to pass through over a network.

In this tutorial, we’ll discuss several ways of transforming an XML Document object into a string in Java.

2. Example

Suppose we have the following Document object:

Document document = // ...

This Document object represents XML content in memory:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <child1>This is child element 1</child1>
    <child2>This is child element 2</child2>

Now, we need to convert this XML Document object into a Java string.

3. Using XML Transformation APIs

The javax.xml.transform package in Java includes classes and interfaces for performing XML transformations. One of its capabilities is the conversion of an XML Document object into a string representation. The following code demonstrates how to use the javax.xml.transform package to parse this XML Document object to a Java string:

public void givenXMLDocument_whenUsingTransformer_thenConvertXMLToString()
throws TransformerException {  
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    StringWriter stringWriter = new StringWriter();
    transformer.transform(new DOMSource(document), new StreamResult(stringWriter));
    String result = stringWriter.toString();

    assertTrue(result.contains("This is child element 1"));
    assertTrue(result.contains("This is child element 2"));

We first instantiate a TransformerFactory and a Transformer, used for XML transformation. Then, we construct a StringWriter to store the transformed XML in text form. Then, the transform() method changes the XML Document, and we can save it into the result string using the stringWrite.toString() method.

4. Using Java XMLBeans

Converting between XML Document and string is easy and flexible using the XmlBeans approach in the Java XML manipulation world. We use XmlObject.Factory.parse(document), which parses the XML Document into an XmlObject for subsequent operative activities:

public void givenXMLDocument_whenUsingXmlBeans_thenConvertXMLToString() {
    try {
        XmlObject xmlObject = XmlObject.Factory.parse(document);

        XmlOptions options = new XmlOptions();

        String xmlString = xmlObject.xmlText(options);

        xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" + xmlString;

        assertTrue(xmlString.contains("This is child element 1"));
        assertTrue(xmlString.contains("This is child element 2"));
    } catch (XmlException e) {

In the above test method, we parse the document into an XmlObject using XmlOptions to customize output formatting such as pretty-printing, namespaces, etc. Moreover, asserts are done to establish that a resulting XML string contains an XML declaration and particular XML elements and element contents.

5. Conclusion

In this tutorial, we discuss how to convert an XML Document object to a string in Java.

As always, the complete code samples for this article can be found over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.