Adding nodes and attributes to XML using E4X

Initially I was under assumption E4X is something very complex to work with having habituated to AS2 style and was still using XMLDocument. However gave E4x a try.. My aim is to add an attribute and a node under it. Base XML what I have is below.

var xmlData:XML = ;

if(xmlData.chapter.@label =="CN1"){
trace("label exists");
trace("label doesn't exist");
xmlData.chapter.@label = "CN1";

trace("xmlData "+ xmlData.toXMLString());

trace what comes out is

label doesn't exist

Adding nodes with conditions, this really saves lot of effort and lines of code

if(xmlData.chapter.(@label =="CN1").lesson.@label == "CN1LN1"){
trace("its there ");
trace("oops missing");
xmlData.chapter.(@label =="CN1").lesson.@label = "CN1LN1";
xmlData.chapter.(@label =="CN1").lesson.(@label = "CN1LN1").appendChild("Some dummy text");
trace("xmlData "+ xmlData.toXMLString());

Below is the trace result

oops missing

Some dummy text

xmlData Some dummy text
facebooktwittergoogle_plusredditpinterestlinkedinmailby feather

Leave a Comment

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

Disclaimer: All published content views of author.