Wednesday, September 15, 2010

Working with XML data using AS3: Part 6

In this tutorial we will learn how to change XML object's data.

E4X gives us the ability to change XML Object's data on the go with Actionscript 3. It is important that you understand that we are only changing the local XML Object, not the XML document file (catalog2.xml) itself.

There are a few ways to change data. For example, we can change an attribute's value. Say, we don't have any Chairs anymore in stock, so we're going to sell Stools instead. To change something, you first have to define what and where do you want to do it. You'll need to check for the existing value first too, to make sure its the right product you want to change. If you do not check and have multiple attributes/tags with that name, you'll get an error... For example, if I simply try to change all the @name attributes at once - I will get an error.

Here is how you do it:

function parseXML(xmlData:XML):void
{
trace("Parsing:");
trace("-----------------");
xmlData.product.(@name=="Chair").@name = "Stool"
trace(xmlData);
}

I checked whether it is the Chair product I'm changing, and changed its name to Stool. Then I traced the whole document. Results:

XML file loaded!
Parsing:
-----------------
<catalog>
  <product name="Stool" price="$50">Something to sit on</product>
  <product name="Sofa" price="$100">Something soft to sit on</product>
  <product name="Table" price="$70">Something to put stuff on</product>
</catalog>

Now, another situation... We want to change our Chair's price. How do we do that?

function parseXML(xmlData:XML):void
{
trace("Parsing:");
trace("-----------------");
xmlData.product.(@name=="Chair").@price = "$40"
trace(xmlData);
}

The output window shows us that the price has been updated from $50 to $40.

Another way to edit existing data, is to specify which product exactly do you want to edit through the [ ] :

function parseXML(xmlData:XML):void
{
trace("Parsing:");
trace("-----------------");
xmlData.product[1] = "This is the new sofa description. Buy our sofa!"
trace(xmlData);
}

This changes our sofa description. We can change attributes this way too:

function parseXML(xmlData:XML):void
{
trace("Parsing:");
trace("-----------------");
xmlData.product[1].@name = "Couch"
trace(xmlData);
}

You can edit the whole nod and re-write the XML data completely. In the next example, we re-write the second product.

function parseXML(xmlData:XML):void
{
trace("Parsing:");
trace("-----------------");
xmlData.product[1] = <product name="Wardrobe" price="$300">This is where you keep your clothes</product>
trace(xmlData);
}

Output:

XML file loaded!
Parsing:
-----------------
<catalog>
  <product name="Chair" price="$50">Something to sit on</product>
  <product name="Wardrobe" price="$300">This is where you keep your clothes</product>
  <product name="Table" price="$70">Something to put stuff on</product>
</catalog>

But instead of putting the wardrobe in sofa's place, we can just add a new product:

function parseXML(xmlData:XML):void
{
trace("Parsing:");
trace("-----------------");
xmlData.product[xmlData.product.length()] = <product name="Wardrobe" price="$300">This is where you keep your clothes</product>
trace(xmlData);
}

We can also put the wardrobe after sofa and before table.

function parseXML(xmlData:XML):void
{
trace("Parsing:");
trace("-----------------");
xmlData.product[1] += <product name="Wardrobe" price="$300">This is where you keep your clothes</product>
trace(xmlData);
}

Thanks for reading!

Related:

Working with XML data using AS3: Part 1
Working with XML data using AS3: Part 2
Working with XML data using AS3: Part 3
Working with XML data using AS3: Part 4
Working with XML data using AS3: Part 5
Working with XML data using AS3: Part 7
Working with XML data using AS3: Part 8
Working with XML data using AS3: Part 9
Working with XML data using AS3: Part 10

1 comment:

wahlrus said...

An AS3 and XML series in one! Thanks for these. Though I don't code much myself, I've been very curious as to how XML is implemented in AS3, and that's pretty nifty.

Post a Comment