2 Replies Latest reply on Nov 11, 2010 4:35 AM by Adobe-InDesign CS4

    CS4-JS : Read XML file and getting Attributes

    Adobe-InDesign CS4 Level 1

      Dear All,

      How to get the Attributes based on the RootElements.


      For Example:

      //========================== XML File : Start ================================//

      <stag>

           <cust>
              <custname>120</custname>
               <atagst name="alpha" attributename="1" attributevalue="2" sty="First"/>
               <atagst name="beta" attributename="1" attributevalue="5" sty="Second"/>

               <atagst name="gama" attributename="1" attributevalue="2" sty="Third"/>
                <atagst name="theta" attributename="1" attributevalue="5" sty="Fourth"/>

            <cust>

           <cust>
               <custname>121</custname>
                <atagst name="A.alpha" attributename="1" attributevalue="2" sty="First"/>
                <atagst name="A.beta" attributename="1" attributevalue="5" sty="Second"/>

               <atagst name="A.gama" attributename="1" attributevalue="2" sty="Third"/>
                <atagst name="A.theta" attributename="1" attributevalue="5" sty="Fourth"/>

            <cust>

           <cust>
               <custname>122</custname>
                <atagst name="B.alpha" attributename="1" attributevalue="2" sty="First"/>
                <atagst name="Bbeta" attributename="1" attributevalue="5" sty="Second"/>

               <atagst name="B.gama" attributename="1" attributevalue="2" sty="Third"/>
                <atagst name="B.theta" attributename="1" attributevalue="5" sty="Fourth"/>

            <cust>

      </stag>

      //==========================  XML File : End ================================//


      Here I want to check through Java Script Code [InDesign]


      //======================== Script : Starts ====================================//

      var myEveryName = new Array();

      traverse(roots);

        for(var Element_name=0; Element_name<myEveryName.length; Element_name++)
               {
                        if(myEveryName[Element_name] == "customername")
                                {
                                        custname.push(myEveryContent[Element_name]);
                                }
         
                      if(myEveryName[Element_name] == "applytagstyle")
                           {
                                  Aname.push(myEveryAttributes[Element_name][0]);
                                   Aattributename.push(myEveryAttributes[Element_name][1]);
                                   Aattributevalue.push(myEveryAttributes[Element_name][2]);
                                   Asty.push(myEveryAttributes[Element_name][3]);
                                  
                               }
            }

      function traverse(tree) {
          myEveryName.push(tree.name()); 
          if(tree.elements().length() > 0) {
              for(var i=0; i<tree.elements().length(); i++) {
                  traverse(tree.elements()[i]);
              }
          }
      }

      //========================  XML File : End ====================================//



      Everything working fine, but I couldn't get attribute values. Please check the below example


      For Example:


      If you check first root element in above xml code


      i need the output like


      custname=120

      name=alpha,beta,gama,theta

      attributename=1,1,1,1

      attributevalue=2,5,2,5

      sty=first,second,third,fourth


      custname=121

      ...

      ...

      ...


      Please any one can help me and give me the solutions.



      Thanks & Regards

      T.R.Harihara SudhaN






        • 1. Re: CS4-JS : Read XML file and getting Attributes
          PankChat Level 1

          Few questions:

           

          1. Your XML is not well formed.

          2. Secondly, I do not see any relation of XML with script. For instance, I do see any elements "customername", "applytagstyle" in input.

          3. Either you have not provided the complete source or either your dummy XML is incorrect.

           

          Anyways, having a quick look, I guess you are trying to get specific attributes values from XML tree. I will try to give you a kick start though you will be required to customized the script as per requirement (for instance rearranging the attribute values in array and so forth). Otherwise please try to post complete inputs.

           

          #include "glue code.jsx"

           

          //Get the attribute values of all elements

          main();

          function main(){

          if (app.documents.length != 0){

          var myDoc = app.activeDocument;

           

          var myRuleSet = new Array (

          new findObjAttribute("//*")

          );

           

          with(myDoc){

          var elements = xmlElements;

          __processRuleSet(elements.item(0), myRuleSet);

           

          }

          }

          else{

          alert("You have no document open!");

          exit();

          }

           

           

          function findObjAttribute(XPATH){

          this.name = "findObjAttribute";

          this.xpath = XPATH;

          this.apply = function(myElement, myRuleProcessor)

          {

          var elmName=myElement.markupTag.name;

          with(myElement){

          try {

          var Name=myElement.xmlAttributes.itemByName("name").value;

          var AttName=myElement.xmlAttributes.itemByName("attributename").value;

          var AttValue=myElement.xmlAttributes.itemByName("attributevalue").value;

          var AttSty=myElement.xmlAttributes.itemByName("sty").value;

          $.writeln("Name: "+Name);

          $.writeln("AttributeName: "+AttName);

          $.writeln("AttributeValue: "+AttValue);

          $.writeln("Sty: "+AttSty);

               } catch(e){};

           

          }

          return true;

          }

          }

          }

           

          This will just print the values JavaScript console.

           

          HTH,

          Pankaj Chaturvedi

          • 2. Re: CS4-JS : Read XML file and getting Attributes
            Adobe-InDesign CS4 Level 1

            Dear Pankchat

             

            Many thanks for your quick responce....

             

            I just wanted to explain you  that this is not InDesign Flow XML, this is separately we are Loading the xml. Please check the below code.

             

            Like:

            //======================== Script : Starts ====================================//

            var myEveryName = new Array();

             

            var Fpath = File(app.filePath +"/Config/MXPApSty.xml");

            if(File(Fpath).exists)
              {
              Fpath.open("r");
              var Cont= Fpath.read();
              var roots = new XML(Cont);
              traverse(roots);
               }

             

            for(var Element_name=0; Element_name<myEveryName.length; Element_name++)
                     {
                              if(myEveryName[Element_name] == "customername")
                                      {
                                              custname.push(myEveryContent[Element_name]);
                                      }
               
                            if(myEveryName[Element_name] == "applytagstyle")
                                 {
                                        Aname.push(myEveryAttributes[Element_name][0]);
                                         Aattributename.push(myEveryAttributes[Element_name][1]);
                                         Aattributevalue.push(myEveryAttributes[Element_name][2]);
                                         Asty.push(myEveryAttributes[Element_name][3]);
                                        
                                     }
                  }

            function traverse(tree) {
                myEveryName.push(tree.name()); 
                if(tree.elements().length() > 0) {
                    for(var i=0; i<tree.elements().length(); i++) {
                        traverse(tree.elements()[i]);
                    }
                }
            }

            //========================  Script : End ====================================//

             

            Hope you understand my problem. Please let me know your suggestion.

             

            Thank you by advance.
            T.R.Harihara SudhaN