-
Senior Member
How to access XML Attribute Names in E4X?
It would seem that I am not connecting the dots somewhere. I am trying to get the string names of the attributes in an xml node by looping though it's attributes, but and can't seem to make it work with E4X.
Example:
In AS2, it's easily done like so:
PHP Code:
var myxDoc:XML = new XML('<node id="1" name="Some Name" type="typeString" />');
var mynode:XMLNode = myxDoc.firstChild;
for (var prop in mynode.attributes)
{
trace(prop + " = " + mynode.attributes[prop])
}
// outputs the following:
id = 1
name = Some Name
type = typeString
So in AS3, I know we can use the XMLDocument, but it's quite interesting how the documentation recommends using E4X instead of legacy. Never the less, this also works:
PHP Code:
var myxDoc:XMLDocument = new XMLDocument('<node id="1" name="Some Name" type="typeString" />');
var mynode:XMLNode = myxDoc.firstChild;
for (var prop in mynode.attributes)
{
trace(prop, "=", mynode.attributes[prop])
}
// and this outputs:
name = Some Name
type = typeString
id = 1
However, when trying to do this with an XML / XMLList objects, this sort of thing happens:
PHP Code:
var myxml:XML = <top><node id="1" name="Some Name" type="typeString" /></top>;
var list:XMLList = myxml.node[0].attributes();
for (var prop in list)
{
trace("prop = ", prop);
}
// outputs the iteration count
prop = 0
prop = 1
prop = 2
//and a for each loop...
for each (var prop in list)
{
trace("prop = ", prop);
}
// outputs the value of each item i.e. list[index]
prop = 1
prop = Some Name
prop = typeString
So, is there a way with the new XML and XMLList objects, to loop through a particular node's attributes and be able to retrieve name:value pairs? I have also tried playing with the dot notation but I can still only get the index (int) and not the name (string):
using same myxml:XML object as above example:
PHP Code:
for (var prop in myxml.node.@*)
{
trace("prop = ", prop);
}
// outputs indexes not attribute names
prop = 0
prop = 1
prop = 2
Have I just missed something?
TJ
The Early Bird may get the worm, but the second mouse to the trap gets the cheese...
-
.name() method is what you are looking for.
var myxml:XML = <top><node id="1" name="Some Name" type="typeString" /></top>;
var list:XMLList = myxml.node[0].attributes();
for each (var prop in list)
{
trace("prop = ", prop.name() + ", value = " + prop);
}
prop = id, value = 1
prop = name, value = Some Name
prop = type, value = typeString
-
Senior Member
Thanks! That is exactly what I was looking for. Don't know how I missed it, but I did.
The Early Bird may get the worm, but the second mouse to the trap gets the cheese...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|