E4X : XML tweaking

One of the lesser known features of Pentaho Data Integration 3.1 was the inclusion of E4X a.k.a EMCAScript for XML.  Up until recently I didn’t really have time to look more closely to the details behind it.  Today someone asked how to set an attribute in an XML string using JavaScript.  Well, this is how you do it…

Suppose we have the following piece of XML in a String field called “input”:

<foo>bar</foo>

Now we want to set an attribute in <foo> with a certain id=5 :

var foo = new XML(input);

foo.@id = 5;

var result = foo.toXMLString()

As you can see from the syntax, you need to construct a special XML object before you can work with it like we did.

By the way, the result is :

<foo id=”5″>bar</foo>

Our friends over at Mozilla have compiled an extensive set of explanations and examples on this page. Great job Mozilla!

Until next time,

Matt

2 thoughts on “E4X : XML tweaking”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.