Read XML data into Javascript Node -
i'm trying play around creating own xml json converter. 1 problem, can't seem figure out how read xml data in. code below failed attempt read in string. there way read in file xml?
function xmltojson(xml) { // create return object var obj = {}; debugger; if (xml.nodetype == 1) { // element // attributes if (xml.attributes.length > 0) { obj["@attributes"] = {}; (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) { // text obj = xml.nodevalue; } return obj; }; var xml = "<subject><story><unique_id>665c43</unique_id><story_url>http://this ismystory.com</story_url></story></subject>"; xmltojson(xml);
maybe try xml2js if don't want use jquery.
here example xml2js github:
var parsestring = require('xml2js').parsestring; var xml = "<root>hello xml2js!</root>" parsestring(xml, function (err, result) { console.dir(result); });
Comments
Post a Comment