Dynamically add element / attribute to a XML using E4X

Written for: Flex, ActionScript 3.0

To dynamically add element / attribute to a XML; assume that it already exists in the XML and using the .(dot) operator, pass a value to it.

Here’s an example. Consider the following XML:

var albumXML:XML =
<tracks>
	    <song tracknumber="1">
	        <title>Orgasm Addict</title>
	        <artist>Buzzcocks</artist>
	    </song>
	    <song tracknumber="2">
	        <title>What Do I Get?</title>
	        <artist>Buzzcocks</artist>
	    </song>
	    <song tracknumber="3">
	        <title>I Dont Mind</title>
	        <artist>Buzzcocks</artist>
	    </song>
	    <song tracknumber="4">
	        <title>Love You More</title>
	        <artist>Buzzcocks</artist>
	    </song>
	</tracks>;

To add an additional element of <genre> to the first <song> node, I’ll use the following line of code.

albumXML.song[0].genre = 'Punk';

Similarly to add an attribute of duration to the first <song> node, I’ll use.

albumXML.song[0].@duration = '2:00';
Now if you trace the XML file, it’ll show as:
trace(albumXML);
/*
<tracks>
  <song tracknumber="1" duration="2:00">
    <title>Orgasm Addict</title>
    <artist>Buzzcocks</artist>
    <genre>Punk</genre>
  </song>
  <song tracknumber="2">
    <title>What Do I Get?</title>
    <artist>Buzzcocks</artist>
  </song>
  <song tracknumber="3">
    <title>I Dont Mind</title>
    <artist>Buzzcocks</artist>
  </song>
  <song tracknumber="4">
    <title>Love You More</title>
    <artist>Buzzcocks</artist>
  </song>
</tracks>
*/

Additionally, you can use the following methods of a XML object.

  • appendChild()
  • prependChild()
  • insertChildAfter()
  • insertChildBefore()
Advertisements
Tagged , ,

6 thoughts on “Dynamically add element / attribute to a XML using E4X

  1. mukesh says:

    thanks a lot !!

  2. Ajay says:

    Thanks, it helped

  3. Mikko says:

    Hi,

    I think that the topic is misleading. How can you say that you are adding an attribute dynamically, when you have to know name of the attribute at compile time? Does anyone know, how to achieve in this? I would need to generate an XML document within as3 and there is no way of knowing anything of document’s structure beforehand. In pseudo:

    element.addAttribute(attributeName, attributeValue);

    I have not yet found a way of doing this with built in XML class collection, so I had to build my own Document, Element, Attribute etc classes for creating document. But now I cannot export for example scandic characters..

    • Scythe says:

      Hi Mikko,

      Seems to be as simple as this:

      var attributeName:String = “someAttribute”;
      var value:String = “someValue”;

      xmlNode.attribute(attributeName)[0] = value;

      where xmlNode is the node where you are willing to add the attribute.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Advertisements