Assuming doc is your instance of org.w3c.dom.Document:

TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
StringWriter writer = new StringWriter();
transformer.transform(new DOMSource(doc), new StreamResult(writer));
String output = writer.getBuffer().toString().replaceAll("\n|\r", "");
Answer from WhiteFang34 on Stack Overflow
🌐
Baeldung
baeldung.com › home › xml › convert string containing xml to org.w3c.dom.document
Convert String Containing XML to org.w3c.dom.Document | Baeldung
June 20, 2025 - In the previous code, we start by creating the necessary elements for the parsing of XML, such as DocumentBuilderFactory and DocumentBuilder. After that, it builds a basic XML schema with an initial node element labeled “root” encompassing another child node element referred to as “element” that has the string “XML document example”. Moreover, the XML output should be as follows:
Discussions

xml - How to convert String to DOM Document object in java? - Stack Overflow
I have a case like getting an XML and convert the XML elements to document object and getting the element values and attributes which i have been created already Here is the piece of code i have t... More on stackoverflow.com
🌐 stackoverflow.com
Is there a more elegant way to convert an XML Document to a String in Java than this code? - Stack Overflow
The transformer API is the only XML-standard way to transform from a DOM object to a serialized form (String in this case). As standard I mean SUN Java XML API for XML Processing. More on stackoverflow.com
🌐 stackoverflow.com
Convert XML-File to string without manipulation or optimization in Java - Stack Overflow
I have some trouble with the JDOM2 whitch i use to work with XML files. I want to convert the XML file to a string without any manipulation or optimization. Thats my Java code to do that: SAXBui... More on stackoverflow.com
🌐 stackoverflow.com
Parsing and finding information in a XML file with JSoup
Seeing as you have already done it. This is how I would have done it, it is only slightly different. String url = "http://xkcd.com/rss.xml"; Document doc = Jsoup.connect(url).get(); Elements links = doc.select("item > description"); for(Element e : links) { String ownText = e.ownText(); Document frag = Jsoup.parseBodyFragment(ownText); Elements select = frag.select("img"); System.out.println(select.attr("src")); } More on reddit.com
🌐 r/javahelp
7
5
September 29, 2015
🌐
How to do in Java
howtodoinjava.com › home › java xml › java convert xml to string – write xml dom to a file
Java Convert XML to String - Write XML Dom to a File
March 9, 2023 - We can then use this string to print in console or log files. public static String convertXmlDomToString(Document xmlDocument) { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer; try { transformer = tf.newTransformer(); // Uncomment if you do not require XML declaration // transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); //A character stream that collects its output in a string buffer, //which can then be used to construct a string.
🌐
Baeldung
baeldung.com › home › java › java string › convert an xml object to a string in java
Convert an XML Object to a String in Java | Baeldung
June 20, 2025 - 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:
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-convert-string-to-xml-document-and-xml-document-to-string
Java Convert String to XML Document and XML Document to String | DigitalOcean
August 3, 2022 - String convertDocumentToString(Document doc): This method will take input as Document and convert it to String. We will use Transformer, StringWriter and StreamResult for this purpose. package com.journaldev.xml; import java.io.StringReader; import java.io.StringWriter; import javax.xml.pa...
🌐
Mkyong
mkyong.com › home › java › java – convert string to xml
Java - Convert String to XML - Mkyong.com
July 29, 2022 - This article shows how to convert a String to an XML document and an XML document back to a String in Java, using DOM parser and JDOM2 parser.
🌐
Blogger
javarevisited.blogspot.com › 2015 › 07 › how-to-read-xml-file-as-string-in-java-example.html
How to Read XML File as String in Java? 3 Examples
May 16, 2023 - Since an XML file is also a file, you can use BufferedReader or FileInputStream to read the content of an XML file as String by using the techniques, I have discussed in my earlier post 3 ways to convert InputStream to String in Java.
Find elsewhere
🌐
GitHub
gist.github.com › 3164030
Convert XML Document to String - Java · GitHub
Convert XML Document to String - Java. GitHub Gist: instantly share code, notes, and snippets.
🌐
Baeldung
baeldung.com › home › java › java string › how to convert org.w3c.dom.document to string in java
How to Convert org.w3c.dom.Document to String in Java | Baeldung
June 20, 2025 - Learn how to create an XML Document from a Java String object and convert it back into a String using the javax.xml.transform package.
🌐
Baeldung
baeldung.com › home › java › java string › parse xml as a string in java
Parse XML as a String in Java | Baeldung
August 15, 2025 - Specifically, we converted an XML string to a character stream and parsed it as an input source. Similarly, we covered how to parse the string as an input byte array stream. Lastly, we also parsed an XML document from an XML file and converted it into a single-line XML string.
🌐
Blogger
self-learning-java-tutorial.blogspot.com › 2018 › 03 › convert-xml-document-to-string.html
Programming for beginners: Convert xml document to string
March 24, 2018 - package com.sample.util; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.io.StringWriter; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; public class XMLUtil { public static String getXMLString(String xmlFilePath) { if (xmlFilePath == null || xmlFilePath.isEmpty()) { throw new IllegalArgumentException("xmlF
🌐
Java Tips
javatips.net › blog › convert-xml-document-to-xml-string-in-java
Convert XML Document To XML String, XML String to XML Document In Java
March 26, 2016 - import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.io.StringReader; import java.io.StringWriter; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.xml.sax.InputSource; public class ConvertXMLStringToXMLDocument_XMLDocumentToXMLString { /** * Convert XML Documen
🌐
Geekality
geekality.net › blog › java-string-with-xml-to-dom-document-to-string-with-xml
Java: String with XML to DOM Document to String with XML | Geekality
The DocumentBuilder can also be reused if you call the reset method between uses, but not across threads 🙂 · public static String toString(Node subject, boolean omitXmlDeclaration, boolean indent) throws Exception { Transformer t = TransformerFactory .newInstance() .newTransformer(); t.setOutputProperty(OutputKeys.INDENT, indent ? "yes" : "no"); t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitXmlDeclaration ?
🌐
Java Code Geeks
javacodegeeks.com › home › core java
How to Parse XML from a String in Java - Java Code Geeks
November 18, 2024 - This article explains two effective ... an InputStream. To parse XML as a String, the key is to convert the String into a source that the DocumentBuilder can process....
🌐
Coderanch
coderanch.com › t › 632186 › java › Java-Convert-String-XML-Document
Java Convert String of XML to Document object is null all the time (Java in General forum at Coderanch)
I have looked at solutions provided [here](xml - How to convert String to DOM Document object in java? - Stack Overflow), [here](How to create a XML object from String in Java? - Stack Overflow) and a few other blogs that give a variation of the same solution.
🌐
Coderanch
coderanch.com › t › 126126 › languages › Document-String
Document to String (XML forum at Coderanch)
In the meantime, I've found another way to convert the XML doc into a string by serializing and then writing to a String, and here's that String: ... Note that the method in which I used your code is called convertXMLToString, and it takes in the XML doc. ... Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch] ... Strange. That would mean that the newTransformer() method would return a null. Well, it shouldn't, according to the javadoc:
🌐
MySkillPoint
myskillpoint.com › convert-string-to-xml-and-xml-to-string-in-java
Convert String to XML and XML to String in Java - MySkillPoint
September 18, 2021 - In the previous example we saw ... to String.For converting the XML Document to String, we will use TransformerFactory , Transformer and DOMSource classes....