Non-jQuery version:

var parseXml;

if (window.DOMParser) {
    parseXml = function(xmlStr) {
        return ( new window.DOMParser() ).parseFromString(xmlStr, "text/xml");
    };
} else if (typeof window.ActiveXObject != "undefined" && new window.ActiveXObject("Microsoft.XMLDOM")) {
    parseXml = function(xmlStr) {
        var xmlDoc = new window.ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = "false";
        xmlDoc.loadXML(xmlStr);
        return xmlDoc;
    };
} else {
    parseXml = function() { return null; }
}

var xmlDoc = parseXml("<foo>Stuff</foo>");
if (xmlDoc) {
    window.alert(xmlDoc.documentElement.nodeName);
}

Since jQuery 1.5, you can use jQuery.parseXML(), which works in exactly the same way as the above code:

var xmlDoc = jQuery.parseXML("<foo>Stuff</foo>");
if (xmlDoc) {
    window.alert(xmlDoc.documentElement.nodeName);
}
Answer from Tim Down on Stack Overflow
Discussions

xml - Converting XMLDocument object to String in Javascript - Stack Overflow
Means the method converted the whole XMLDocument into string, including the very first tag · I don't want this part of the response. Is there any method that does that. Note: I don't want to use the workarounds like "substr" etc. More on stackoverflow.com
🌐 stackoverflow.com
javascript - how to convert xml document object to string? - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... I have xml that I have in a string. I want to change some attributes in it. More on stackoverflow.com
🌐 stackoverflow.com
Convert xml to string with jQuery - Stack Overflow
Are you looking to extract specific ... the xml string that is returned from the AJAX request? ... @Ryan- the former- although I will be saving off the entire xml doc, I need to perform manipulations on it first, so I need to bring it in as xml ... More on stackoverflow.com
🌐 stackoverflow.com
XML parsing of a variable string in JavaScript - Stack Overflow
I have a variable string that contains well-formed and valid XML. I need to use JavaScript code to parse this feed. How can I accomplish this using (browser-compatible) JavaScript code? More on stackoverflow.com
🌐 stackoverflow.com
🌐
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 ...
🌐
C# Corner
c-sharpcorner.com › blogs › xml-parser-in-javascript
XML Parser In JavaScript
March 21, 2023 - XMLSerialzer() object is used to parse the XML to string using the serializeToString method.
🌐
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...
🌐
OpenText
docs.microfocus.com › SM › 9.50 › Hybrid › Content › programming › javascript › reference › javascript_method_xml_toxmlstring.htm
JavaScript method: XML.toXMLString()
Develop > Programming Guide > JavaScript and Service Manager reference > Service Manager defined JavaScript objects > JavaScript object: XML > JavaScript method: XML.toXMLString() This method converts an XML object into a valid XML string.
Find elsewhere
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › XML › Guides › Parsing_and_serializing_XML
Parsing and serializing XML - MDN Web Docs
Loads content from a URL. XML content is returned as a text string which you can parse using DOMParser. ... The precursor to fetch().
🌐
TutorialsPoint
tutorialspoint.com › how-to-convert-xml-to-json-string-in-javascript
How to Convert XML to JSON String in JavaScript?
November 6, 2024 - If you only need to handle simple XML structure a custom parser might be better and here we will manually parse XML tags and attributes. function customXmlToJson(xml) { const obj = {}; const regex = /<([^/]+?)>([^<]+)<\/\1>/g; let match; while ((match = regex.exec(xml))) { obj[match[1]] = match[2]; } return obj; } const xmlString = `<person><name>Pankaj</name><age>20</age><city>Surat</city></person>`; console.log(JSON.stringify(customXmlToJson(xmlString)));
🌐
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. ... ServiceNow Runbook Template: Best Practices for Reliable Deployments in Developer articles yesterday · ServiceNow Integration Tutorial: How Integrations Really Work (Step-by-Step) in Developer articles yesterday · How to make variable mandatory based task field conditions.
🌐
SitePoint
sitepoint.com › blog › javascript › how to convert xml to a json-like javascript object
How to Convert XML to a JSON-Like JavaScript Object — SitePoint
November 6, 2024 - Finally, the structure of the resulting JavaScript object may not be what you expect, especially if your XML data is complex or deeply nested. The DOM Parser is a built-in feature of many web browsers that can be used to parse XML data. To use it, you create a new instance of the DOMParser object and call its ‘parseFromString’ method, passing in your XML data as a string.
🌐
WebDeveloper.com
webdeveloper.com › community › 221227-javascript-amp-xmlconverting-an-xml-file-into-a-string
Javascript & XML…converting an XML file into a string
[code] var parser = new DOMParser(); var xmldom = parser.parseFromString("<root><child/></root>", "text/xml"); // as you can see the above code only takes a string, no other options here.
🌐
GitHub
github.com › nashwaan › xml-js
GitHub - nashwaan/xml-js: Converter utility between XML text and Javascript object / JSON text.
To convert JavaScript object to XML text, use js2xml().
Starred by 1.3K users
Forked by 179 users
Languages   JavaScript 97.3% | CSS 1.4% | JavaScript 97.3% | CSS 1.4%
Top answer
1 of 10
331

Updated answer for 2017

The following will parse an XML string into an XML document in all major browsers. Unless you need support for IE <= 8 or some obscure browser, you could use the following function:

function parseXml(xmlStr) {
   return new window.DOMParser().parseFromString(xmlStr, "text/xml");
}

If you need to support IE <= 8, the following will do the job:

var parseXml;

if (typeof window.DOMParser != "undefined") {
    parseXml = function(xmlStr) {
        return new window.DOMParser().parseFromString(xmlStr, "text/xml");
    };
} else if (typeof window.ActiveXObject != "undefined" &&
       new window.ActiveXObject("Microsoft.XMLDOM")) {
    parseXml = function(xmlStr) {
        var xmlDoc = new window.ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = "false";
        xmlDoc.loadXML(xmlStr);
        return xmlDoc;
    };
} else {
    throw new Error("No XML parser found");
}

Once you have a Document obtained via parseXml, you can use the usual DOM traversal methods/properties such as childNodes and getElementsByTagName() to get the nodes you want.

Example usage:

var xml = parseXml("<foo>Stuff</foo>");
alert(xml.documentElement.nodeName);

If you're using jQuery, from version 1.5 you can use its built-in parseXML() method, which is functionally identical to the function above.

var xml = $.parseXML("<foo>Stuff</foo>");
alert(xml.documentElement.nodeName);
2 of 10
93

Update: For a more correct answer see Tim Down's answer.

Internet Explorer and, for example, Mozilla-based browsers expose different objects for XML parsing, so it's wise to use a JavaScript framework like jQuery to handle the cross-browsers differences.

A really basic example is:

var xml = "<music><album>Beethoven</album></music>";

var result = $(xml).find("album").text();

Note: As pointed out in comments; jQuery does not really do any XML parsing whatsoever, it relies on the DOM innerHTML method and will parse it like it would any HTML so be careful when using HTML element names in your XML. But I think it works fairly good for simple XML 'parsing', but it's probably not suggested for intensive or 'dynamic' XML parsing where you do not upfront what XML will come down and this tests if everything parses as expected.

🌐
ReqBin
reqbin.com › code › javascript › h6wipkyl › javascript-parse-xml-example
How to Parse XML in JavaScript?
September 1, 2023 - DOMParser is a JavaScript API introduced to parse XML or HTML, creating a DOM representation of the parsed document. The main method is parseFromString(), which takes a string of XML data and returns a DOM object representing the parsed document.