2 Replies Latest reply on Mar 19, 2015 2:42 AM by beh_gras_beh

    Getting XML child elements

    beh_gras_beh Level 1

      Dear Indesign scripters,

       

      This script is making from all the XML tags ParagraphStyles.

      But it refuses to take the child elements. Do I have to use XPath expression? And how?

       

      function main() {  
        var xes, n, doc, ps, xe;  
        if ( !app.documents.length ) return;  
        doc = app.activeDocument;
        xes = doc.xmlElements[0].xmlElements;  
        n = xes.length;  
        while ( n-- ) {  
        xe = xes[n]; 
        st = doc.paragraphStyles.itemByName (xe.markupTag.name );  
        !st.isValid && doc.paragraphStyles.add({name:xe.markupTag.name});  
        }  
      }  
        
        
      main(); 
      

       

      The XML

       

      <Workbook>  
           <Element_A_01>  
                <Element_A_01></Element_A_01>  
           </Element_A_01>  
           <Element_B_02>  
                <Element_B_02></Element_B_02>  
           </Element_B_02>  
      </Workbook> 
      

       

      Greetings from Holland

        • 1. Re: Getting XML child elements
          Manan Joshi Level 4

          The code creates two paragraph styles namely Element_A_01 and Element_B_02. These two are created for the immediate child nodes of the Workbook node. Now it seems that you are trying to create pstyles for all the nodes in the XML, which in your case would still be these two as Element_A_01 has a child node that is also named Element_A_01 and the same is the case for Element_B_02.

           

          If i get your point right then you need to create 4 pstyles if all the nodes have different names, but that is not working. For this you will have to recursively traverse each node that you get from the code xes[n].

           

          The code be something like this

          1. function main() {   
          2.   var xes, n, doc, ps, xe;   
          3.   if ( !app.documents.length ) return;   
          4.   doc = app.activeDocument; 
          5.   xes = doc.xmlElements[0].xmlElements; //This gives the immediate child nodes of the Workbook node, hence the length in the next statement is 2  
          6.   n = xes.length;   
          7.   while ( n-- ) {   
          8.   xe = xes[n];   //Here you need to recursively traverse the node xe
          9.   //The no of child elements of xe can be obtained as xe.xmlElements.length
          10.   //First element can be obtained by xe.xmlElements[0]
          11.   }   
          12. }   
          13.    
          14.    
          15. main();

          Hope this solves the issue

          • 2. Re: Getting XML child elements
            beh_gras_beh Level 1

            Thanks for the help,

             

            It's a bit of an struggle for me (noob) but i'm getting there.

            Now I've got it with this script, but is this the right way?

             

            function main() {  
              var xes, n, doc, ps, xe;  
              if ( !app.documents.length ) return;  
              doc = app.activeDocument;
              xes = doc.xmlElements[0].xmlElements;
              n = xes.length;  
              while ( n-- ) {  
              xe = xes[n];
                try
                {
              st = doc.paragraphStyles.itemByName (xe.markupTag.name ); 
              !st.isValid && doc.paragraphStyles.add({name:xe.markupTag.name}); 
            
            
              for(var i=0;i<xe.xmlElements.length;i++)
              st = doc.paragraphStyles.itemByName (xe.xmlElements[0].markupTag.name ); 
              !st.isValid && doc.paragraphStyles.add({name:xe.xmlElements[0].markupTag.name});
            
            
                 }
                catch(e){}
              }
            }
            main();
            
            
            

             

            Greetings for Holland