2 Replies Latest reply on Oct 5, 2015 4:44 AM by Upendra_sengar

    How to convert XMl to JSON object in jsx ?

    Upendra_sengar Level 2

      Does any one know how can i convert an xml file to json object. any help or any jsx library that can help please let me know if possible  ?

       

      Thanks,

      upendra

        • 1. Re: How to convert XMl to JSON object in jsx ?
          Silly-V Adobe Community Professional

          I looked at this and saw that this is a very broad topic

           

          JSON and XML Conversion

           

          You can probably re-purpose some code out there for converting XML to JSON using your desired convention.
          As for me, I will paste this little experimentation up here for my own basic reference in the future, and I hope it may be useful to you as well.
          It will use an example XML file which is structured thus:

          <pets>
            <cats family="feline" tail="yes">
            <name>Tom</name>
            <name>Garfield</name>
            </cats>
            <dogs family="canine" tail="yes">
            <name>Scruffy</name>
            <name>Rex</name>
            <name>Ralph</name>
            </dogs>
          </pets>
          

          And some examples of manipulation using ExtendScript:

          function test(){
              var xmlFile = Folder.desktop.openDlg("Open XML file", "XML: *.xml");
              if(xmlFile != null){
                  xmlFile.open('r');
                  var myXML = XML(xmlFile.read());
                  xmlFile.close();
                 
                  $.writeln("\n--------------CONSOLE OUTPUT-------------");
                  $.writeln("Root is called " + myXML.name() + " and has " + myXML.children(0).length() + " nodes.");
                  // Root is called pets and has 2 nodes.
                  $.writeln("");
                 
                  $.writeln("There are " + myXML.dogs.name.length() + " dogs names listed.");
                  // There are 3 dogs names listed.
                  $.writeln("");
                 
                  var desiredProperty = "dogs";
                  $.writeln("There are " + myXML[desiredProperty].name.length() + " dogs names listed.");
                  // There are 3 dogs names listed.
                  $.writeln("");
                 
                  var desiredProperties = ["dogs", "cats", "hamsters"], i = 0, thisProperty;
                  while(i < desiredProperties.length){
                      thisProperty = desiredProperties[i];
                      $.writeln("There are " + myXML[thisProperty].name.length() + " " + thisProperty  + " names listed.");
                      i++;
                  }
                  // There are 3 dogs names listed.
                  // There are 2 cats names listed.
                  // There are 0 hamsters names listed.
                  $.writeln("");
                 
                  var dogsArray = [], i = 0;
                  while(i < myXML.dogs.name.children().length()){
                      dogsArray.push(myXML.dogs.name.children()[i]);
                      i++;
                  }
                  $.writeln("The dog names are:\n" + dogsArray.join("\n"));
                  //The dog names are:
                  //Scruffy
                  //Rex
                  //Ralph
                  $.writeln("");
                 
                  $.writeln("The dog attributes are: \"" + myXML.dogs.attributes() + "\"");
                  // The dog attributes are: canineyes        <--- not good
                  $.writeln("");
                 
                  function getAttributes(node){
                      function AttributeObject(){};
                      AttributeObject.prototype = {
                          printData : function(){
                              var str = "", all;
                              for(var all in this){
                                  if(this.hasOwnProperty(all)){
                                      str += (all + " : " + this[all] + ",");
                                  }
                              }
                              return str.replace(/,$/,"");
                          }
                      };
                      var result = new AttributeObject(), i = 0, length = node.attributes().length();
                      if(length){
                          while(i < length){
                              result[node.attributes()[i].name()] = node.attributes()[i].toString();
                              i++;
                          }
                      }
                      return result;
                  };
             
                  $.writeln("The dog attributes are: \"" + getAttributes(myXML.dogs).printData().replace(/,/g,", ") + "\"");
                  //The dog attributes are: "family : canine, tail : yes"
                  $.writeln("");
                 
                  $.writeln("------------END CONSOLE OUTPUT-----------");
              }
          }
          test();
          
          • 2. Re: How to convert XMl to JSON object in jsx ?
            Upendra_sengar Level 2

            I could create this script with your help which is a generic XML to JSON converter just pass xml obj in the function.
            Thanks for your suggestion Silly-V

             

            function xmlToJson(xml) {
              // Create the return object
              var obj = {};
              if (xml.nodeKind() == "element") {
              if (xml.attributes().length() > 0) {
              for (var j = 0; j < xml.attributes().length(); j++) {
              var attributeName = xml.attributes()[j].name();
              obj[attributeName] = String(xml.attributes()[j]);
              }
              }
              } else if (xml.nodeKind() == "text") {
              obj['text'] = xml.text();
              }
              if (xml.children()) {
              for (var i = 0; i < xml.children().length(); i++) {
              var item = xml.child(i);
              if (xml.children()[i].nodeKind() == "text") {
              obj['text'] = xml.children()[i].toString();
              } else {
              var nodeName = item.name();
              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;
            };
            

             

             

            Thanks,

            upendra sengar