Use LoadXml Method of XmlDocument;

string xml = "<head><body><Inner> welcome </head> </Inner> <Outer> Bye</Outer></body></head>";
xDoc.LoadXml(xml);
Answer from Waqar on Stack Overflow
🌐
JSON Formatter
jsonformatter.org › xml-to-string-converter
Best XML to String Converter Online
XML to String Converter Online tool is the easiest way to convert XML to String / Text. Save online and Share.
🌐
Code Beautify
codebeautify.org › xml-stringify-online
XML Stringify Online to stringify XML to xml string
XML Stringify is easy to use tool to convert XML to XML String while escaping special characters.
People also ask

In JavaScript how do you convert XML to a string
divtoXMLString method transforms an XML object into an XML string To move across an XML document you must use the other XML get methodsbrbrdiv
🌐
scholarhat.com
scholarhat.com › home
Convert string to xml and xml to string using javascript
How can XML data be converted to a string
divWe begin by creating a TransformerFactory and a Transformer for XML transformation The modified XML is then stored in text form using a StringWriter The transform method then modifies the XML Document which we may store into the result string using stringWritebrbrbrdiv
🌐
scholarhat.com
scholarhat.com › home
Convert string to xml and xml to string using javascript
What is the tool for converting XML to strings
divXML Stringify is a simple utility for converting XML to XML Strings and escaping special charactersbrbrbrdiv
🌐
scholarhat.com
scholarhat.com › home
Convert string to xml and xml to string using javascript
🌐
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.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.tran
🌐
Mkyong
mkyong.com › home › java › java – convert string to xml
Java - Convert String to XML - Mkyong.com
July 29, 2022 - package com.mkyong.xml.tips; import org.jdom2.Document; import org.jdom2.JDOMException; import org.jdom2.input.SAXBuilder; import org.jdom2.output.Format; import org.jdom2.output.XMLOutputter; import javax.xml.XMLConstants; import java.io.IOException; import java.io.StringReader; import java.io.StringWriter; // JDOM2 Parser public class ConvertStringToXmlJDom2 { final static String str = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<company>\n" + " <staff id=\"1001\">\n" + " <name>mkyong</name>\n" + " <role>support</role>\n" + " <salary currency=\"USD\">5000</salary>\n" + " <!-- for speci
Top answer
1 of 4
32

If you just want to put the content of a String in a file, it doesn't really matter whether it is actually XML or not. You can skip the parsing (which is a relatively expensive operation) and just dump the String to file, like so:

public static void stringToDom(String xmlSource) 
        throws IOException {
    java.io.FileWriter fw = new java.io.FileWriter("my-file.xml");
    fw.write(xmlSource);
    fw.close();
}

If you want to be on the safe side and circumvent encoding issues, as pointed by Joachim, you would need parsing however. Since its good practice to never trust your inputs, this might be the preferable way. It would look like this:

public static void stringToDom(String xmlSource) 
        throws SAXException, ParserConfigurationException, IOException {
    // Parse the given input
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(new InputSource(new StringReader(xmlSource)));

    // Write the parsed document to an xml file
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    DOMSource source = new DOMSource(doc);

    StreamResult result =  new StreamResult(new File("my-file.xml"));
    transformer.transform(source, result);
}
2 of 4
2
public static void stringToDom(String xmlSource) throws SAXException, ParserConfigurationException, IOException, TransformerException{
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(new InputSource(new StringReader(xmlSource)));
    // Use a Transformer for output
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();

    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(new File("c:/temp/test.xml"));
    transformer.transform(source, result);
}  

Source : http://docs.oracle.com/javaee/1.4/tutorial/doc/JAXPXSLT4.html

🌐
InterSystems
community.intersystems.com › post › string-xml
%String to XML | InterSystems Developer Community | ObjectScript|XML|Caché
I have a similar requirement where I get an XML string as part of an email and I have managed to get the XML string from the input.
Find elsewhere
🌐
ScholarHat
scholarhat.com › home
Convert string to xml and xml to string using javascript
September 22, 2025 - The DOMParser object has a method called parseFromString that parses a string that contains either HTML or XML and produces a document object (HTMLDocument or XMLDocument). This allows you to directly load and manipulate XML data within your ...
🌐
Online String Tools
onlinestringtools.com › convert-xml-to-string
Convert XML to a String – Online String Tools
Quickly convert a JSON stringified string to a regular string. ... Quickly extract all string data from a HTML page. ... Quickly extract all string data from an XML document.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › standard › linq › parse-string
How to parse a string - LINQ to XML - .NET | Microsoft Learn
September 15, 2021 - You can parse a string with XElement.Parse to create an XML tree in C# and Visual Basic, and you can create an XML tree with XML literals in Visual Basic.
🌐
Android Developers
developer.android.com › app architecture › string resources
String resources | App architecture | Android Developers
See Formatting and Styling, below, for information about to properly style and format your strings. ... Keyword. A value indicating when this string should be used. Valid values, with non-exhaustive examples in parentheses: ... <?xml version="1.0" encoding="utf-8"?> <resources> <plurals name="numberOfSongsAvailable"> <!-- As a developer, you should always supply "one" and "other" strings.
🌐
Red Gate Software
red-gate.com › home › converting string data to xml and xml to string data
Converting String Data to XML and XML to String Data | Simple Talk
August 24, 2021 - I assign a string value-an XML fragment-to the @string data type, then set the value of @xml to equal @string. Because SQL Server can implicitly convert an NVARCHAR value to an XML value, the assignment is very straightforward, which I confirm ...
🌐
W3Schools
w3schools.com › xml › xml_parser.asp
XML Parser
All major browsers have a built-in XML parser to access and manipulate XML. The XML DOM (Document Object Model) defines the properties and methods for accessing and editing XML. However, before an XML document can be accessed, it must be loaded into an XML DOM object. All modern browsers have a built-in XML parser that can convert text into an XML DOM object. This example parses a text string into an XML DOM object, and extracts the info from it with JavaScript:
🌐
ServiceNow Community
servicenow.com › community › developer-forum › how-do-i-convert-xml-object-to-string-requirement-is-to-replace › m-p › 2093269
How do I convert XML object to String ? Requirement is: to replace < and > with < and > respectively and make the string back to XML format. Sample XML is shown as below.
June 11, 2018 - var xmlString = responseBody.toString(); var xmlClean = global.JSUtil.unescapeText(xmlString); JSUtils is an OOB Script Include already present in every instance. ... Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact. Thanks Ankur · Regards, Ankur Certified Technical Architect || 9x ServiceNow MVP || ServiceNow Community Leader ... replace works only with String, the Xml in question is an XML Object.
🌐
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 - Review the two most common approaches to converting an XML string to an XML document in Java.