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
🌐
ScholarHat
scholarhat.com › home
Convert string to xml and xml to string using javascript
September 22, 2025 - You can easily convert a string to XML and vice versa. You can transform your data from text to structured XML format with just a few lines of code. Let's get into the JavaScript Tutorial magic. To learn more and enhance your JavaScript skills, be sure to enroll in our Free Javascript Course ...
🌐
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...
🌐
GitHub
gist.github.com › 905833
javascript : convert a string to XML DOM · GitHub
javascript : convert a string to XML DOM · Raw · gistfile1.js · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
C# Corner
c-sharpcorner.com › blogs › xml-parser-in-javascript
XML Parser In JavaScript
March 21, 2023 - The above image shows the XML, which is converted from the string. var text = "<author>" + "<FirstName>Bob</FirstName>" + "<LastName>Ross</LastName>" + "</author>"; var parser = new DOMParser(); var xmlDoc = parser.parseFromString(text, "text/xml"); console.log(xmlDoc); console.log(xmlDoc.all) var text = "<author>" + "<FirstName>Bob</FirstName>" + "<LastName>Ross</LastName>" + "</author>"; All the properties are used to get a list of nodes in XML.
🌐
Medium
medium.com › @tariibaba › javascript-convert-json-to-xml-80caf3148886
How to Convert JSON to XML in JavaScript | Medium
September 16, 2022 - import { json2xml } from 'xml-js';const jsonObj = { name: 'Garage', cars: [ { color: 'red', maxSpeed: 120, age: 2 }, { color: 'blue', maxSpeed: 100, age: 3 }, { color: 'green', maxSpeed: 130, age: 2 }, ], };const json = JSON.stringify(jsonObj);const xml = json2xml(json, { compact: true, spaces: 4 });console.log(xml); ... <name>Garage</name> <cars> <color>red</color> <maxSpeed>120</maxSpeed> <age>2</age> </cars> <cars> <color>blue</color> <maxSpeed>100</maxSpeed> <age>3</age> </cars> <cars> <color>green</color> <maxSpeed>130</maxSpeed> <age>2</age> </cars> Before using xml-js, we'll need to install it in our project.
🌐
SitePoint
sitepoint.com › javascript
Convert string into XML object - JavaScript - SitePoint Forums | Web Development & Design Community
August 30, 2014 - i have a simple string that looks something like this… var xml = " Bob Smith bob@smith.com "; i’d like to take this string and convert it so i can run things like… var names = xml.getElementsById("name"); var num_names = names.length;
Find elsewhere
🌐
npm
npmjs.com › package › xml-parse-from-string
xml-parse-from-string - npm
May 18, 2017 - Parses the string as XML and returns the root element as a DOM element, so you can do operations similar to document.getElementById, document.getElementsByTagName, and so forth.
      » npm install xml-parse-from-string
    
Published   May 18, 2017
Version   1.0.1
Author   Matt DesLauriers
🌐
ReqBin
reqbin.com › code › javascript › h6wipkyl › javascript-parse-xml-example
How to Parse XML in JavaScript?
September 1, 2023 - To parse XML in JavaScript, you can use DOMParser API, which converts XML strings into a structured Document Object Model (DOM). The DOM model enables easy traversal and manipulation of XML data.
🌐
OpenText
docs.microfocus.com › SM › 9.50 › Hybrid › Content › programming › javascript › reference › javascript_method_xml_toxmlstring.htm
JavaScript method: XML.toXMLString()
You must use the other XML get methods to navigate through an XML document. ... There are no arguments for this method. ... A string or null. The method returns a string containing valid XML or returns null if the current node is not an XML object. ... This example converts an XML object into a valid XML document. ... var xmlObject = system.users print( "This is the original XML object:\n" + xmlObject ); var xmlString = xmlObject.toXMLString(); print( "This is the XML object after conversion to a string:\n" + xmlString );
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.

🌐
Google
discuss.google.dev › google cloud › apigee
How to convert a string to XML using the javascipt model of Apigee - Apigee - Google Developer forums
September 14, 2015 - I am converting an XML to JSON using Javascript. The XML comes in as a string and I am not able to convert it into an XML object. Can anyone guide me in this regard. As of now I have tried var parser=new DOMParser(); x…
🌐
jQuery
api.jquery.com › jQuery.parseXML
jQuery.parseXML() | jQuery API Documentation
Description: Parses a string into an XML document. ... jQuery.parseXML uses the native parsing function of the browser to create a valid XML Document.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › API › XMLSerializer
XMLSerializer - Web APIs | MDN
November 3, 2025 - This example just serializes an entire document into a string containing XML. ... This involves creating a new XMLSerializer object, then passing the Document to be serialized into serializeToString(), which returns the XML equivalent of the document.