2 Replies Latest reply on Mar 16, 2016 12:42 PM by scottRaysNI

    How to access XML elements by name in Extendscript??

    clrx01

      I'm almost done the script that I've been working on, but something has been nagging me since I started. When I did start, I looked at the JS Tools Guide CS5 that comes with the extendscript ide to check how to access XML elements, children, attributes etc. It says this:

       

      The XML object represents an XML element node in an XML tree. The topmost XML object for an XML file

      represents the root node. It acts as a list, which contains additional XML objects for each element. These in

      turn contain XML objects for their own member elements, and so on.

      The child elements of an element tree are available as properties of the XML object for the parent. The

      name of the property corresponds to the name of the element. Each property contains an array of XML

      objects, each of which represents one element of the named type.

       

      So basically it converts the XML into JSON. And you can access the properties like so:

       

      <book category="COOKING">

           <title lang="en">The Boston Cooking-School Cookbook</title>

           <author>Fannie Merrit Farmer</author>

           <year>1896</year>

           <price>49.99</price>

      </book>

       

      The Javascript statement bookstoreXML.book; returns the entire list of books.

      The statement bookstoreXML.book[0]; returns the XML object for the first book.

      The statement bookstoreXML.book[0].author; returns all authors of the first book.

       

      A couple pages down it talks about Retrieving contained elements using children(), elements(), descendants().

       

      So now I look through the XML Object properties and I see that I can use:

       

      xmlObj.child (which) which A String, the element name, or a Number, a 0-based index into this node’s child array.

       

      or

       

      xmlObj.descendants ([name])

      name Optional. A String, the element name to match. If not provided, matches all

      elements.

       

      or

       

      xmlObj.elements (name);

      name Optional. A String, the element name to match. If not provided, matches all

      elements.

       

      This is an excerpt of an XML I was working with:

       

      <ROW xmlns="http://www.filemaker.com/fmpdsoresult"

           MODID="16"

           RECORDID="11128">

         <Sign_Type>251.dr</Sign_Type>

         <fm:Location xmlns:fm="http://www.filemaker.com/fmpdsoresult" xmlns="">5-5024</fm:Location>

         <Line1>Zone

      Floor

      3

      Patient Rooms

      R532 - R436 even

      Patient Rooms

      R522 - R446 even

      Xavier Elevators

      Zone

      Patient Rooms

      R537 - 5757 odd

      Main Elevators

      Zone</Line1>

      </ROW>

       

      Extendscript will not give me an anything when I try to access elements by name. Instead I have to access by index, which works, but I'd rather search for names!

       

      Actually the console log returns: <![CDATA[]]>

       

      What am I doing wrong!?

        • 1. Re: How to access XML elements by name in Extendscript??
          Dirk Becker Level 4

          First, those E4X XML objects are definitely no JSON (plain data) - they have a multitude of methods, even for tasks that would easily be implemented as property (e.g. the length() function), and they also bind xml element and attribute names onto the objects, allowing to target a multitude of XML nodes with a single statement. Or did you mean your script with that "it"?

           

          Anyway, as you found out the ExtendScript XML object handles namespaces mostly by hiding them from you.

          You could play around with global namespace settings, see the JavaScript tools guide.

          You could also explicitly specify a namespace. This works for me:

           

          $.writeln(myXML["fm:Location"]);

           

          For simple use I had most success with a brute force approach that just strips the namespaces.

           

          function removeAllNamespace(xml)

          {

                    var ns =new Namespace();

                    var d=xml.descendants();

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

              d[i].setNamespace(ns);

          }

          • 2. Re: How to access XML elements by name in Extendscript??
            scottRaysNI Level 1

            I wanted to thank you for posting the "removeAllNamespace()" function! It helped solve an issue I was dealing with.

            Cheers,

            Scott