• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How to access XML elements by name in Extendscript??

New Here ,
Nov 04, 2013 Nov 04, 2013

Copy link to clipboard

Copied

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!?

TOPICS
Scripting

Views

4.0K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Participant , Mar 16, 2016 Mar 16, 2016

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

Cheers,

Scott

Votes

Translate

Translate
Adobe
Guide ,
Nov 11, 2013 Nov 11, 2013

Copy link to clipboard

Copied

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.setNamespace(ns);

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 16, 2016 Mar 16, 2016

Copy link to clipboard

Copied

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

Cheers,

Scott

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 24, 2020 Sep 24, 2020

Copy link to clipboard

Copied

LATEST

function importXML(xmlFile) { if (xmlFile.open('r')) { xmlText = xmlFile.read(); xmlFile.close(); var x = new XML(xmlText); return x } } var myXML_Hot=File("/Volumes/xml_data/2021_XML_script/10123-000-21.xml"); var myXML_Hot=File("/Users/bagonterman/Desktop/10123-000-21.xml"); // paragraphCount = myXML_Hot.evaluate( '/Jobs//CoverPlans/text()', myXML_Hot, null, XPathResult.ANY_TYPE, null ); x = importXML(myXML_Hot); //$.writeln(x["Binding"]) var d=x.descendants('Binding');///this works var myXpath=x.xpath ('/Jobs//ColorList/*');///this works var myXpath=x.xpath ('/Jobs//CoverPlans/text()');///this works/Jobs//CoverPlans/text() $.writeln(myXpath)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines