5 Replies Latest reply on Jun 30, 2011 1:47 PM by vze26m98

    Convert XMLElement into E4X XML Object?

    vze26m98

      Wow. Didn't think I'd be back here so soon...

       

      Picking up on yesterday's discussion, but different aspects; a couple (I hope) quick questions:

       

      Context: I have a script that produces an array of XMLElements from a user selection, and I want to process the XML still further.

       

      1) I haven't found a means to convert an XMLElement into an E4X XML object, aside from serializing it out to a file. Have I missed it, or am I supposed to use XMLRules here if I want to avoid traversing the DOM?

       

      2) (Sorry... Need to work harder on my second question!)

       

      Thanks much!

       

      Charles

        • 1. Re: Convert XMLElement into E4X XML Object?
          vze26m98 Level 1

          OK, I see I need to use XMLRules (I think: Can't convert XMLElements to E4X XML objects. XMLRules work on XMLElements).

           

          But if I do, my selection is ultimately an Array of XMLElements, and so I still have to iterate through the array to apply my XMLRules. It'd be great to assign the selected XMLElements to a parent XMLElement, then hand it off once to an XMLRule, but I'm stumped, or more likely, too tired:

           

          var theDocument = app.documents[app.activeDocument.index];
          var tagSelection = theDocument.xmlTags.add("SELECTION");
          var xmlSelection = theDocument.xmlElements[0].xmlElements.add(tagSelection);
          
          var theSelection = theDocument.selection;
          var items = theSelection.length;
          var selectedXML = new XMLElement(items);
          
          for (var i = 0; i < items; i++) {
            selectedXML[i] = theSelection[i].associatedXMLElement;
          }
          
          // I know this is silly, but it's what I'd like to do.
          xmlSelection.add(selectedXML);
          

           

          Any thoughts appreciated!

           

          Best, Charles

          • 2. Re: Convert XMLElement into E4X XML Object?
            John Hawkinson Level 5

            I told you it was going to hurt...

             

            You can move XMLElements. So you can move them around under the document root XML Element and perhaps put them all into a subtree you want to process. This may not help if having them in their original position was important, because you'll have to put them back, and that might be hard (well...).

             

            I believe in your other thread I had suggested you build the list of associatedXMLElements and then in your XMLRule compare the current element to your list. I think that still works.

             

            You're correct that you cannot convert XMLElements to E4X objects, E4X has no DOM relationship. But you can export to a file and read the file back in. And it's even conceivable that might be appropriate... [though ugly]

            • 3. Re: Convert XMLElement into E4X XML Object?
              vze26m98 Level 1

              John Hawkinson wrote:

               

              I believe in your other thread I had suggested you build the list of associatedXMLElements and then in your XMLRule compare the current element to your list. I think that still works.

               

              Hi John,

               

              Yes, this is the current process. I was just hoping I could push more of the traversal into an XMLRule, and gain more speed. I guess I should be happy with what I've got: sub 1/2-second performance.

               

              I did experiment with moving and file export, but again, I think they're oriented to element-at-a-time operations, so my array of XMLElements only adds to the ugliness. Iteration, iteration... (BTW, the Adobe JS Scripting Guide, uses "iteration" instead of "traversal" with respect to the XML structures they discuss, cf. p.180. ;-) )

               

              Thanks for your help once again,

               

              Charles

              • 4. Re: Convert XMLElement into E4X XML Object?
                John Hawkinson Level 5
                BTW, the Adobe JS Scripting Guide, uses "iteration" instead of "traversal" with respect to the XML structures they discuss, cf. p.180. ;-)

                But it uses traversal in the next sentence!

                • 5. Re: Convert XMLElement into E4X XML Object?
                  vze26m98 Level 1

                  Correct Answer!

                   

                  Best, C.