简体   繁体   中英

XML To JSON Conversion with attributes

I am trying to convert the XML to JSON.Here am facing challenge my xml have @attributes name as "value" in all tag. while convert into xml to JSON i am using the below code.

var xml = "<Message><id value="123"></id><type value="Test"></type></Message>"
var json = XMLtoJSON(xml, ["type", "space", "xmlns", "html"]);
var result = JSON.stringify(json)    
function XMLtoJSON(xml, ignored) {
          var r, children = xml.*, attributes = xml.@*, length = children.length();
          if(length == 0) {
            r = xml.toString();
          } else if(length == 1) {
            var text = xml.text().toString();
            if(text) {
              r = text;
            }
          }
          if(r == undefined) { 
            r = {};
            for each (var child in children) {
             var name = child.localName();
             var json = XMLtoJSON(child, ignored);
             var value = r[name];
             if(value) {
               if(value.length) {
                 value.push(json);
               } else {
                 r[name] = [value, json]
               }
             } else {
               r[name] = json;
             }
            }
          }
          if(attributes.length()) {
            var a = {}, c = 0;
            for each (var attribute in attributes) {
              var name = attribute.localName();
              if(ignored && ignored.indexOf(name) == -1) {
                a["_" + name] = attribute.toString();
                c ++;
              }
            }
            if(c) {
              if(r) a._ = r;
              return a;
            }
          }

          return r;
        }

Input XML :

<Message><id value="123"></id><type value="Test"></type></Message>

Actual Output:

{"id":{"_value":"123"},"type":{"_value":"Test"}}

Expected Output:

{"id":"123","type":"Test"}

Guide me where am missing the part to get the expected output.

Regards, nkn1189

do you think if you do this way will work for you?

put your actual output from that parser to this function:

function convertToExpectedOutput(obj){
    var result = {}
    for (var i in obj){
        if (i == "_value")
           return obj[i];
        else
           result[i] = convertToExpectedOutput(obj[i])
    }
    return result;
}

convertToExpectedOutput(actualOutput)

So, for your array, hange the convertToExpectedOutput to this way and it will give the expected result:

function convertToExpectedOutput(obj){
    var result = {}
    for (var i in obj){
        if (i == "_value")
           return obj[i];
        else
        if (Array.isArray(obj[i])){
           result[i] = [];
           arr = obj[i]
           for (var j in arr)
              result[i].push(convertToExpectedOutput(arr[j]))
        }  
        else
           result[i] = convertToExpectedOutput(obj[i])
    }
    return result;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM