Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tool (javascript) to convert a XML string to JSON [closed]

What is the best javascript function/plugin/library to convert a XML string to JSON.

I found that tool : http://www.thomasfrank.se/xml_to_json.html, but it does not like strings starting with 0. i.e.: 005321 get converted to 2769 (not cool :( )

My question, what is the best javascript function/plugin/library to convert a XML to JSON?

EDIT : Someone tried one that works fine?

like image 518
David Laberge Avatar asked Oct 14 '11 15:10

David Laberge


People also ask

Can we convert XML to JSON in JavaScript?

To convert XML text to JavaScript object, use xml2js() . To convert XML text to JSON text, use xml2json() .

How do I convert XML data to JSON format?

To convert an XML document to JSON, follow these steps: Select the XML to JSON action from the Tools > JSON Tools menu. Choose or enter the Input URL of the XML document. Choose the path of the Output file that will contain the resulting JSON document.

Can we convert string to JSON in JavaScript?

Convert String to JSON Using eval() The eval() function in JavaScript is used to take an expression and return the string. As a result, it can be used to convert the string into JSON.


3 Answers

This function has worked pretty well for me:

xmlToJson = function(xml) {
    var obj = {};
    if (xml.nodeType == 1) {                
        if (xml.attributes.length > 0) {
            obj["@attributes"] = {};
            for (var j = 0; j < xml.attributes.length; j++) {
                var attribute = xml.attributes.item(j);
                obj["@attributes"][attribute.nodeName] = attribute.nodeValue;
            }
        }
    } else if (xml.nodeType == 3) { 
        obj = xml.nodeValue;
    }            
    if (xml.hasChildNodes()) {
        for (var i = 0; i < xml.childNodes.length; i++) {
            var item = xml.childNodes.item(i);
            var nodeName = item.nodeName;
            if (typeof (obj[nodeName]) == "undefined") {
                obj[nodeName] = xmlToJson(item);
            } else {
                if (typeof (obj[nodeName].push) == "undefined") {
                    var old = obj[nodeName];
                    obj[nodeName] = [];
                    obj[nodeName].push(old);
                }
                obj[nodeName].push(xmlToJson(item));
            }
        }
    }
    return obj;
}

Implementation:

var jsonText = JSON.stringify(xmlToJson(xmlDoc)); // xmlDoc = xml dom document
like image 163
James Johnson Avatar answered Nov 05 '22 11:11

James Johnson


Another small library for XML <=> JSON is https://github.com/abdmob/x2js

like image 38
abdolence Avatar answered Nov 05 '22 11:11

abdolence


If you're willing to use jQuery, there is:

http://www.fyneworks.com/jquery/xml-to-json/

$.get("http://jfcoder.com/test.xml.php", function(xml){
    var json = $.xml2json(xml);
    $('pre').html(JSON.stringify(json)); // To show result in the browser
});

Using:

<nums>
 <num>00597</num>
 <num>0059</num>
 <num>5978</num>
 <num>5.978</num>
</nums>

Outputs:

{"num":["00597","0059","5978","5.978"]}

http://jfcoder.com/test.php

like image 2
Jared Farrish Avatar answered Nov 05 '22 12:11

Jared Farrish