1 Reply Latest reply on Feb 5, 2016 1:23 AM by Loic.Aigon

    read xml in reverse order

    aviR Level 1

      How to read xml elements in reverse order from the indesign structure panel??

      I want to read the parent element of a particular element in a loop

        • 1. Re: read xml in reverse order
          Loic.Aigon Adobe Community Professional

          Hi,

           

          First of all, if you only need to retrieve string data from an element, I would definitively recomment using a XML object instead. It's much much faster.

           

          var reverseXMLthroughObject = function() {
              var doc, xf, xo, cs, c, n, arr = [];
              
              if (!app.documents.length
              || !app.activeDocument.xmlElements[0].xmlElements.length) {
                  alert("Nothing to work with…");
                  return;
              }
          
              doc = app.activeDocument;
              xf = File ( Folder.temp+"/tmp.xml");
              doc.exportFile ( ExportFormat.XML, xf );
              xf.open('r');
              xo = XML(xf.read());
              xf.close();
              xf.remove();
              
              cs = xo.xpath("*/*");
              n = cs.length();
              
              while (n--) arr[arr.length]=String(cs[n]);
              
              return arr.join(",");
          }
          
          reverseXMLthroughObject();
          

           

          However if you need to do work with xmlElements inside the indesign structure, you may try this way.

           

          var reverseXMLThroughStructure = function() {
              var doc, root, cs, c, n, arr = [];
              
              if (!app.documents.length
              || !app.activeDocument.xmlElements[0].xmlElements.length) {
                  alert("Nothing to work with…");
                  return;
              }
          
              doc = app.activeDocument;
              root = doc.xmlElements[0];
              cs = root.evaluateXPathExpression("./*");
              n = cs.length;
              
              while (n--) cs[n].texts[0].isValid && arr[arr.length]=cs[n].contents;
              
              return arr.join(",");
          }
          
          reverseXMLThroughStructure();
          

           

          Another approach is XMLRules.

           

          Loic

          www.ozalto.com