Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML to JSON javascript function not working

I pulled this function from another stackoverflow question(link below). I want to be able to convert the XML that I have(included as a HTML comment) to a JSON format. I thought this snippet would work but it keeps giving this error:

Uncaught TypeError: Object cat.xml has no method 'hasChildNodes'

I still trying wrap my head around javascript so I can't figure this out. I am sorry if this has been answered but I searched and can't find an answer anywhere.Here is the original question

Tool (javascript) to convert a XML string to JSON

and here is a jsbin with what I am trying to do and with the error showing. Thank you.

http://jsbin.com/ujoPECU/1/edit

var xmlToJson = function (xml) {
    'use strict';
    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;
};
var jsonText = JSON.stringify(xmlToJson("cat.xml")); // xmlDoc = xml dom document
console.log(jsonText);
like image 918
Chad Watkins Avatar asked Apr 16 '26 00:04

Chad Watkins


2 Answers

The problem is that you're trying to parse the string "cat.xml" as an XML document. I would suggest reading up on XML documents in Javascript but you essentially need to get the contents of your XML file, parse it into an XML document, then run it through your xmlToJson function (which actually returns an object).

var xmlString = "<a><b>C</b></a>";
var parser = new DOMParser();
var xml = parser.parseFromString(xmlString, "text/xml");
var obj = xmlToJson(xml);
like image 76
Mike Cluck Avatar answered Apr 18 '26 15:04

Mike Cluck


The code snippet you have here looks like it comes from David Walsh's blog at https://davidwalsh.name/convert-xml-json. The xml parameter is supposed to be a Titanium.XML.DOMDocument. So, you can't simply pass in "cat.xml".
I am not sure what the scope of your project was, but I found the rss-to-json Node library helpful for my mobile project.

like image 24
froggythefrog Avatar answered Apr 18 '26 15:04

froggythefrog



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!