Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
1

How to convert XMl to JSON object in jsx ?

Participant ,
Oct 01, 2015 Oct 01, 2015

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

TOPICS
Scripting
2.0K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Participant , Oct 05, 2015 Oct 05, 2015

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().name();

  obj[attributeName] = String(xml.attributes());

  }

  }

  } else if (xml.nodeKind() == "text")

...
Translate
Adobe
Valorous Hero ,
Oct 01, 2015 Oct 01, 2015

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;

            $.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++;

        }

        $.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().name()] = node.attributes().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();

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Oct 05, 2015 Oct 05, 2015
LATEST

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().name();

  obj[attributeName] = String(xml.attributes());

  }

  }

  } 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().nodeKind() == "text") {

  obj['text'] = xml.children().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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines