3 Replies Latest reply on Feb 22, 2012 6:11 AM by கற்பனை (Imagine)

    How to Get a List of All XMLElements (Follow up to previous posting)

    mlavie Level 1

      (CS5, Win7, 64Bit)

       

      I need a collection (or array, etc.) of all the XMLElement objects in a given document in flat form (just like the "Structure Tree"'s hierarchichal view - only flatened).

       

      I could not find any "normal" way (i.e., built-in property or method) to get at the information. Instead, I found that I had to recurse through the entire XMLElement tree, starting with the Root Element. For a large document, this can take close to a ten minutes, and sometimes I have to do this for an InDesgin Book containing over ten enormous INDD files.

       

      After "கற்பனை Imagine" responded to my previous post about how to get all the XMLTags, I assumed that myDocument.xmlElements would return a collection of all the XMLElements in the Document. Unfortunately, it does not - it returns only the Root XMLElement. Thus, I have to recurse therough the hierarchy of XMLElements under the Root XMLElement - which takes a very long time.

       

      There HAS to be a better way, right?

       

      Would it be better were I to recurse through all PageItems in the document and then recurse through each PageItem's XMLElement tree? I assume that wouldn't make a big difference either way.

       

      I'm open to suggestions...

       

       

      TIA,

      mlavie

        • 1. Re: How to Get a List of All XMLElements (Follow up to previous posting)
          கற்பனை (Imagine) Level 3

          Try this,  It's given the flattern of the XMLElements from the Root tag.

           

          var doc = app.activeDocument;

          var myroot = doc.xmlElements[0];

          var a = myroot.evaluateXPathExpression ("//*");

          var b = a.length;

          var xmlObjArray = new Array();

          while (b--) {

              xmlObjArray.push(a[b--]);

              }

          I Hope this will helps to you. 

          • 2. Re: How to Get a List of All XMLElements (Follow up to previous posting)
            John Hawkinson Level 5

            mlavie:

            I could not find any "normal" way (i.e., built-in property or method) to get at the information. Instead, I found that I had to recurse through the entire XMLElement tree, starting with the Root Element. For a large document, this can take close to a ten minutes, and sometimes I have to do this for an InDesgin Book containing over ten enormous INDD files.

            What do you intend to do with this flat layout of XMLElements? That probably matters a great deal.

             

            If performance is really at play, you can try using XML Rules instead of interacting with the DOM from JavaScript. But they are complicated and confusing and more suited to declarative programming.

             

            Imagine suggests:

            var a = myroot.evaluateXPathExpression ("//*");
            ...
                xmlObjArray.push(a[b--]);
            

             

            Why not stop there? evaluateXPathExpression already returns an array, why iterate over it and push its elements into a new array? What's wrong with the return value?

            • 3. Re: How to Get a List of All XMLElements (Follow up to previous posting)
              கற்பனை (Imagine) Level 3

              John Hawkinson wrote:

               

              Why not stop there? evaluateXPathExpression already returns an array, why iterate over it and push its elements into a new array? What's wrong with the return value?

               

              sorry for the array pushing code,  I'm little bit confused because working in some automation projects simultineously I  reply to forum questions.  Thanks for highlighting my mistakes John.