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");
}else{
trace("label doesn't exist");
xmlData.chapter.@label = "CN1";
}

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

trace what comes out is

label doesn't exist
xmlData

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 ");
}else{
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
xmlData

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.