2 Replies Latest reply on Dec 3, 2015 2:43 AM by pav55

    populate selected tag from xml into dropdownlist SUI

    pav55 Level 1

      hi guys,

      I'm trying to display xml 1 tag type values only in dialogbox dropdownlist.

      I can read and display content but whatever I'm trying to do to get wanted result getting the same results - dropdown displays all content with tags from file as a line (equal dropdown item) multiple by amount of signs or every sign from file appear in dropdown as item.

       

      my xml stucture is:

      <colours>

        <colour default="true">

          <name>color name 1</name>

          <value>my cmyk value 1</value>

        </colour>

        <colour default="true">

          <name>color name 2</name>

          <value>my cmyk value 2</value>

        </colour>

      </colours>

      do you know maybe how I can display only <name> of every colour as an dropdownlist item?

      will appreciate any help...my idea to construct question to uncleG finished

       

      the part of code responsible for reading xml is:

       

      var colourNames = [];

      var file_to_read = File("/myFilePath/test.xml");

      var content;

      file_to_read.open('r');

      content = file_to_read.read();

           //var my_XML_object = null;

           //my_XML_object =  XMLList(content).toSource();

       

      for (x = 0; x < content.length; x++) { 

          var xmlitem = content[x]; 

         colourNames.push (xmlitem);   

      };

       

      commented lines are something I've try as well, maybe will be useful.

      Thank you

        • 1. Re: populate selected tag from xml into dropdownlist SUI
          steverocket2 Level 1

          Hi pav,

           

          you need to read the contents of the file and then create a JavaScript XML Object from it.

          Then you have access to the elements of the xml:

           

          this.file.open("r");

          var curFileString = this.file.read();

          this.XML = new XML(curFileString);

           

          The JavaScript XML Element is not the XMLElement for the InDesign document.

          Stefan

          • 2. Re: populate selected tag from xml into dropdownlist SUI
            pav55 Level 1

            Hi Stefan,

            thank you for your answer and time.

            Have it already solved.

             

            You right it wasn't necessary to create array but just display data.

             

            maybe will be useful for somebody in the future (there are not so many complete samples around www. lots of info you have to guess) so solution looks like that:


            xml structure:

            <?xml version="1.0" encoding="UTF-8"?>

            <colours>

              <colour default="true">

                <itemName>my colour name will be display in dropdown list</itemName>

                <myC>0</myC>

              <myM>100</myM>

              <myY>100</myY>

              <myK>65</myK>

              </colour>

              <colour default="true">

                <itemName>my colour name2 will be display in dropdown list</itemName>

                <myC>0</myC>

              <myM>10</myM>

              <myY>10</myY>

              <myK>25</myK>

              </colour>

            </colours>

             

             

            jsx

            //---read xml before dialog open---//

            var prefXMLFile = File("/myFilePath/test.xml");

            var myResult = prefXMLFile.open("r", undefined, undefined);

            if(myResult == true){

            var xmlstr = prefXMLFile.read();

            }

            else

            {

            alert("Something wrong with xml file - call the police :)")

            }

            prefXMLFile.close();

            var xmlobj = new XML (xmlstr);

            var myitems= xmlobj.colour.length(); //---getting colour item which will be populated in dropdown in dialogbox

             

            //-- and now what tried to do was display itemName in dropdown and keep another 4 values to work with later

             

            //-- adding dropdown to dialogbox selected group:

             

            var myDD = colour_group.add ("dropdownlist", undefined, undefined);

               for (i=0;i<myitems;i++)

                {

                var curitem= xmlobj.colour[i].itemName.toString();

                myDD.add("item",curitem);

                }

             

            //--after setup all parameters in dialogbox and close it in rest part of the script I'm getting rest of the values as numbers

             

            var c= parseInt(xmlobj.colour[myDD.selection.index].myC.toString());

            var m= parseInt(xmlobj.colour[myDD.selection.index].myM.toString());

            var y= parseInt(xmlobj.colour[myDD.selection.index].myY.toString());

            var k= parseInt(xmlobj.colour[myDD.selection.index].myK.toString());

             

            hope it will helps to somebody.

            (sorry for not using code frame but dont know how)

             

            thanks again Stefan

            pav