6 Replies Latest reply on Mar 29, 2016 7:02 AM by Loic.Aigon

    Export xml attribute values

    karthiks94217840 Level 2

      Dear java expert,

       

      I was try to script export attribute value in the .txt format. I have attached screenshot for your reference.  Please suggestion me friends.

       

      Screen Shot 2016-03-26 at 11.58.38 AM.png

      I am using system: Mac, indesign Cs6.

       

      Advance Thanks to all

        • 1. Re: Export xml attribute values
          karthiks94217840 Level 2

          Dear friends,

           

          Please tell any one suggestion in my above question?

           

          Thanks,

          • 2. Re: Export xml attribute values
            karthiks94217840 Level 2

            Dear java kings,

             

            Please tell any one suggestion in my above question?

             

            Advance Thanks to all,

            • 3. Re: Export xml attribute values
              karthiks94217840 Level 2

              Dear friends,

               

              I have share my java coding in below, based on the above question.

               

              var myDocument = app.activeDocument;

              var log=new File(app.activeDocument.filePath+"/log.html");

              log.encoding = "UTF-8";

              log.open("w");

              log.writeln("<html><body><table border=1>");

              log.writeln("<tr><td>Text(In template)</td><td>Found(Href Tag)</td></tr>");

              FindEmail(myDocument);

              log.writeln("</table></body></html>");

              log.close();

              alert("process Completed");

              function FindEmail(elm)

              {

                  for (var i = 0; i < elm.xmlElements.length; i++)

                  {

                      XMLelementName=elm.xmlElements[i].markupTag.name.toString();

                     if(XMLelementName=="img")

                      {

                           for(j=0; j<elm.xmlElements[i].xmlAttributes.length; j++) 

                           {

                              var cc =  elm.xmlElements[i].contents

                               //alert(cc);

                               var aa = elm.xmlElements[i].xmlAttributes[j].name

                                if(aa=="src")

                                   {

                                       //alert (aa);

                                        var bb =  elm.xmlElements[i].xmlAttributes[j].value;

                                       log.writeln("<tr><td>"+ cc +"</td><td>"+  elm.xmlElements[i].xmlAttributes[j].value +"</td></tr>");

                                        //alert (cc  + "~~" + aa+ " ~~ "  + bb);

                                    }

                               }

                      }

                      FindEmail(elm.xmlElements[i]);

                    

                  }

                     

              }

               

              Thanks,

              • 4. Re: Export xml attribute values
                Loic.Aigon Adobe Community Professional

                Hi there is more interesting and performing imho:

                 

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

                 

                 

                FWIW,

                 

                Loic

                Ozalto | Productivity Oriented - Loïc Aigon

                • 5. Re: Export xml attribute values
                  karthiks94217840 Level 2

                  HI Loic.Aigon,

                   

                  Thank you so match for your advance script. Very help full for me.

                   

                  Just one question: Where is you are mention(add) the image tags for <img> in the script?

                   

                  Example for my coding: if(XMLelementName=="img")

                   

                  Please explain me friend.

                   

                  Thanks

                  • 6. Re: Export xml attribute values
                    Loic.Aigon Adobe Community Professional

                    just edit the xpath:

                     

                    //img/@*


                    Loic