Archive for October, 2011

I wanted to create a list of Strings from only the attributes of an XMLList, and I wanted them separated with carriage returns.

The "toXMLString()" method does this automatically on attributes.
Attributes are typed as XMLList.

 
var myXmlList:XMLList=XMLList(<B><P name="Orange"/><P name="Grape"/><P name="Pear"/></B>).P;
trace(myXmlList..@name.toXMLString());
//Outputs multiple values of all "name" attributes separated with carriage returns automatically.
Orange
Grape
Pear
 

Putting the values of an XMLList as an Array might be useful also.

Being separated by carriage returns makes it easy to convert to an Array by using the String "split" method.

Wrap this into a function for easy use.

 
function xmllistToArray(list:XMLList):Array
             {
                 return list.toXMLString().split("\n");
             }
 

Vote in HexoSearch

Comments 1 Comment »

Recently I was working with an XMLList variable and I wanted to add a new XML node to an XMLList.
My first thought was to just use the "appendChild" method as you would with an XML object.
I wasn't really aware of how to do this, and I did not see an XMLList method listed for appending childs in the AS3 documentation.
So I "googled" the term "E4X specification" and read about the "compound assignment operator +=".
Wow using "+=" was really simple.

In the example below I was trying to add a new PAGE node to the existing "PAGE" XMLList....







Before when I used "appendChild". Error!
Even if my XMLList had one child, it would not be the result I wanted.

 
var doc:XML=
<BOOK>
	<PAGE />
	<PAGE />
	<PAGE />
</BOOK>;
 
trace("\nOriginal...");
trace(doc.toXMLString());
doc.PAGE.appendChild(<PAGE />);
trace("\nChanges...");
trace(doc.toXMLString());
 
\\Trace Output:
Original...
<BOOK>
  <PAGE/>
  <PAGE/>
  <PAGE/>
</BOOK>
TypeError: Error #1086: The appendChild method only works on lists containing one item.
	at XMLList/http://adobe.com/AS3/2006/builtin::appendChild()
	at Untitled_fla::MainTimeline/Untitled_fla::frame1()
 







Using the compound assignment operator "+=" gives the result I was looking for. The new PAGE node is added to the XMLList.

 
var doc:XML=
<BOOK>
	<PAGE />
	<PAGE />
	<PAGE />
</BOOK>;
 
trace("\nOriginal...");
trace(doc.toXMLString());
doc.PAGE+=<PAGE />; //doc.PAGE=doc.PAGE+<PAGE />; works just as well.
trace("\nChanges...");
trace(doc.toXMLString());
 
\\Trace Output:
Original...
<BOOK>
  <PAGE/>
  <PAGE/>
  <PAGE/>
</BOOK>
 
Changes...
<BOOK>
  <PAGE/>
  <PAGE/>
  <PAGE/>
  <PAGE/>
</BOOK>
 
Vote in HexoSearch

Comments 2 Comments »

Thanks for visiting www.keith-hair.net