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.
🌐
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
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
Can I convert Node.js to XML automatically?
Yes. DocuWriter.ai's AI-powered converter analyzes your Node.js code and generates equivalent XML code. It handles syntax differences (C-style (JavaScript) to Markup (SGML-based)), type system changes (Dynamic, weak (JavaScript) 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 β€Ί nodejs-to-xml-code-converter
Node.js to XML Converter β€” AI-Powered Code Translation
Should I rewrite from scratch or use a converter for Node.js 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. Node.js (event-driven, asynchronous, functional) and XML (declarative, markup) have different paradigms, so some architectural changes may be needed.
🌐
docuwriter.ai
docuwriter.ai β€Ί nodejs-to-xml-code-converter
Node.js to XML Converter β€” AI-Powered Code Translation
🌐
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%
🌐
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 Β·
🌐
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.
Find elsewhere
🌐
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
🌐
Docuwriter
docuwriter.ai β€Ί nodejs-to-xml-code-converter
Node.js to XML Converter β€” AI-Powered Code Translation
Review the generated code, make any adjustments, and copy it to your project. ... Yes. DocuWriter.ai's AI-powered converter analyzes your Node.js code and generates equivalent XML code. It handles syntax differences (C-style (JavaScript) to Markup (SGML-based)), type system changes (Dynamic, weak (JavaScript) to Schema-typed (optional XSD)), and common library mappings.
🌐
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+"\">"
}
🌐
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..
🌐
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.
🌐
ConvertApi
convertapi.com β€Ί doc-to-xml β€Ί javascript
DOC to XML Conversion JavaScript SDK – Structured Word XML
Convert DOC to XML via JavaScript library. Choose XML type, preserve structure, update TOC and references. No MS Office required.