1 Reply Latest reply on Sep 10, 2016 5:07 AM by karthiks94217840

    loop through each xml element in xml file using extendscript toolkit cs6

    aviR

      hii all

      I have an xml like:

       

      <root>

      <seq path="chapter02/ch02_reader_0.html"/>

        <seq path="chapter02/ch02_reader_1.html"/>

        <seq path="chapter02/ch02_reader_2.html"/>

        <seq path="chapter02/ch02_reader_3.html"/>

      </root>

       

       

      How can i read the value of the path attribute in extendscript?

      I am able to read the file using File.read()

      But how do i get the path attribute values?

      Please suggest

       

      Thanks

        • 1. Re: loop through each xml element in xml file using extendscript toolkit cs6
          karthiks94217840 Level 2

          Hi,

           

          Try this.

           

          You got the all attributes value in the text file.

           

          //*******************************

          var main = function(){ 

            var doc = app.properties.activeDocument, 

            xmlFile = File ( Folder.temp+"/attr.xml" ), 

            xmlObject, 

            xmlAttrs, attrs = [], n = 0, i = 0, 

            sep = "\t"; 

           

            if (!doc) return; 

           

            doc.exportFile ( ExportFormat.XML, xmlFile ); 

           

            if ( !xmlFile.exists ) { 

            alert("Unable to read xml data"); 

            return; 

            } 

           

           

            xmlFile.open('r'); 

            xmlObject = XML ( xmlFile.read() ); 

            xmlFile.close(); 

            xmlFile.remove(); 

           

            xmlAttrs = xmlObject.xpath("//*//@*"); 

           

            n = xmlAttrs.length(); 

           

            while ( i<n ) { 

            attrs [ attrs.length ] = xmlAttrs[i].name()+sep+xmlAttrs[i]; 

            i++; 

            } 

           

            if ( !attrs.length ) { 

            alert("Could'nt find any attributes."); 

            return; 

            } 

           

           

            writeFile ( "Attribute Name"+sep+"Attribute Value"+"\r"+attrs.join("\r") ); 

          }; 

           

           

          var writeFile = function( data ) { 

            var file = File ( Folder.desktop+"/attributes.csv" ); 

            file.open("w"); 

            file.write ( data ); 

            file.close(); 

            file.execute(); 

          main();

           

          //*******************************

           

          Thanks

          KS