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
๐ŸŒ
ConvertSimple
convertsimple.com โ€บ convert-javascript-object-to-xml
Convert JavaScript Object to XML Online - ConvertSimple.com
October 17, 2020 - Convert a JavaScript object or array to XML format with this simple online JavaScript to XML converter tool.
๐ŸŒ
Mozilla
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ XML โ€บ Guides โ€บ Parsing_and_serializing_XML
Parsing and serializing XML - MDN Web Docs
Using one of the following approaches to create an XML document (which is an instance of Document). This example converts an XML fragment in a string into a DOM tree using a DOMParser: js ยท
People also ask

Can I convert JavaScript to XML automatically?
Yes. DocuWriter.ai's AI-powered converter analyzes your JavaScript code and generates equivalent XML code. It handles syntax differences (C-style to Markup (SGML-based)), type system changes (Dynamic, weak to Schema-typed (optional XSD)), and common library mappings. Always review the output โ€” automated conversion is a starting point, not a finished product.
๐ŸŒ
docuwriter.ai
docuwriter.ai โ€บ javascript-to-xml-code-converter
javascript to xml - Code Converter
Should I rewrite from scratch or use a converter for JavaScript to XML?
For small to medium codebases, an AI converter saves significant time by handling routine syntax translation. For large projects, use the converter as a starting point and then refactor to leverage XML-specific patterns. JavaScript (object-oriented, functional, event-driven) and XML (declarative, markup) have different paradigms, so some architectural changes may be needed.
๐ŸŒ
docuwriter.ai
docuwriter.ai โ€บ javascript-to-xml-code-converter
javascript to xml - Code Converter
What JavaScript features don't have a direct XML equivalent?
Some JavaScript features like prototypal inheritance may not map directly to XML. The converter handles these by finding the closest XML equivalent or generating helper code. Be aware that implicit type coercion with == vs === โ€” this can affect conversion results.
๐ŸŒ
docuwriter.ai
docuwriter.ai โ€บ javascript-to-xml-code-converter
javascript to xml - Code Converter
๐ŸŒ
Docuwriter
docuwriter.ai โ€บ javascript-to-xml-code-converter
javascript to xml - Code Converter
... Yes. DocuWriter.ai's AI-powered converter analyzes your JavaScript code and generates equivalent XML code. It handles syntax differences (C-style to Markup (SGML-based)), type system changes (Dynamic, weak to Schema-typed (optional XSD)), and common library mappings.
๐ŸŒ
GitHub
github.com โ€บ scrapinghub โ€บ js2xml
GitHub - scrapinghub/js2xml: Convert Javascript code to an XML document
This makes it easy to extract data embedded in JavaScript code using XPath in a way more robust than just using regular expressions. ... >>> import js2xml >>> >>> jscode = """function factorial(n) { ... if (n === 0) { ...
Starred by 187 users
Forked by 23 users
Languages ย  Python 67.5% | HTML 31.0% | Python 67.5% | HTML 31.0%
๐ŸŒ
npm
npmjs.com โ€บ package โ€บ xml-js
xml-js - npm
February 13, 2019 - To convert JavaScript object to XML text, use js2xml().
      ยป npm install xml-js
    
Published ย  Feb 13, 2019
Version ย  1.6.11
Author ย  Yousuf Almarzooqi
๐ŸŒ
FreeFormatter
freeformatter.com โ€บ json-to-xml-converter.html
Free Online JSON to XML Converter - FreeFormatter.com
This free online tool lets you convert a JSON into an XML file and formats the output with your chosen indentation
Find elsewhere
๐ŸŒ
Medium
medium.com โ€บ @tariibaba โ€บ javascript-convert-json-to-xml-80caf3148886
How to Convert JSON to XML in JavaScript | Medium
September 16, 2022 - We can use the xml-js library to easily convert a JSON string to an XML string in JavaScript.
๐ŸŒ
npm
npmjs.com โ€บ package โ€บ to-xml
to-xml - npm
August 21, 2023 - toXML - Pure JavaScript XML Writer. Latest version: 0.1.11, last published: 3 years ago. Start using to-xml in your project by running `npm i to-xml`. There are 9 other projects in the npm registry using to-xml.
      ยป npm install to-xml
    
Published ย  Aug 21, 2023
Version ย  0.1.11
Author ย  Yusuke Kawasaki
๐ŸŒ
GitHub
github.com โ€บ nashwaan โ€บ xml-js
GitHub - nashwaan/xml-js: Converter utility between XML text and Javascript object / JSON text.
TIP: In non-compact mode, you probably want to set {textKey: 'value', cdataKey: 'value', commentKey: 'value'} to make it more consistent and easier for your client code to go through the contents of text, cdata, and comment. For XML โ†’ JS object / JSON, following custom callback functions can be supplied: var convert = require('xml-js'); var xml = '<foo:Name>Ali</Name> <bar:Age>30</bar:Age>'; var options = {compact: true, elementNameFn: function(val) {return val.replace('foo:','').toUpperCase();}}; var result = convert.xml2json(xml, options); console.log(result); // {"NAME":{"_text":"Ali"},"BAR:AGE":{"_text":"30"}}
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 2
7

There is a non-standard API object: XMLSerializer (it is not standard though is implemented in all but IE browsers).

Its serializeToString method expects DOMNode object to be passed.

var sXML = new XMLSerializer().serializeToString(document.body);

In Internet Explorer there is no way to retrieve proper XML for HTML, unless getting .outerHTML and fixing all problems that come with serialization to HTML (such as missing quotes in attributes, not closed tags etc.)

2 of 2
2

I will have to look into XMLSerializer tomorrow. Here is the code I ended up writing instead, in case anyone is interested (requires prototype and FireBug for unknown nodes):

function extractXML(node) {
    switch (node.nodeType) {
        case 1: return extractNodeXML(node);
        case 2: return extractAttributeXML(node);
        // 3 = Text node
        case 3: return node.nodeValue;
        // 8 = Comment node - ignore
        case 8: return "";
        case 9: return extractDocumentNodeXML(node);
        case 10: return extractDocumentTypeXML(node);
        default: console.log(node); return "Unkwon type: "+node.nodeType;
    }
}
function extractNodeXML(node) {
    var xml = "<"+node.nodeName;
    $A(node.attributes).each(function (node) {xml += " "+extractXML(node)});
    xml +=">"
    $A(node.childNodes).each(function (node) {xml += extractXML(node)});
    xml += "</"+node.nodeName+">"
    return xml;
}
function extractAttributeXML(node) {
    return node.nodeName+"=\""+node.nodeValue+"\""
}
function extractDocumentNodeXML(node) {
    var xml = "<?xml version=\""+node.xmlVersion+"\" encoding=\""+node.xmlEncoding+"\"?>"
    $A(node.childNodes).each(function (node) {xml += extractXML(node)});
    return xml;
}
function extractDocumentTypeXML(node) {
    return "<!DOCTYPE "+node.name+" PUBLIC \""+node.publicId+"\" \""+node.systemId+"\">"
}
๐ŸŒ
GitHub
github.com โ€บ jackrvaughan โ€บ json-to-xml
GitHub - jackrvaughan/json-to-xml: Converts javascript objects / JSON to an xml string ยท GitHub
Converts javascript objects / JSON to an xml string
Starred by 3 users
Forked by 2 users
Languages ย  JavaScript
๐ŸŒ
JSON Formatter
jsonformatter.org โ€บ json-to-xml
Best JSON to XML Converter
JSON to XML is very unique tool for convert JOSN to XML and allows to download, save, share and print JSON to XML data..
๐ŸŒ
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.
๐ŸŒ
Convertjson
convertjson.com โ€บ json-to-xml.htm
JSON To XML Converter
Use this tool to convert JSON into XML format. New- Now supports JSONLines. Enter your JSON or JSONLines data below and Press the Convert button. The output will display below the Convert button.