views:

155

answers:

2

How do I parse an XML document that contains nodes where underscores exist?

<some_xml>
    <child_node>
        <child width_info="" height_info="" />
    </childnode>
</some_xml>

I tried this:

for each (var item:XML in Environment._XMLData.some_xml.child_node.child){
    trace(child.@width_info);
}

But that does'nt seem to work. I can't change the XML either because its from a third party. Any help would be great. Thanks in advance!

A: 

The thing to remember is that .NODE and .ATTRIBUTE are shortcuts for .child("NODE") and .attribute("ATTRIBUTE") respectively (as per Traversing XML structures (c/o the livedocs)

So I would think that something along the lines of the following would work (the code is untested but the theory should be sound).

for each (var item:XML in Environment._XMLData.child("child_node").child){
    trace(child.@width_info);
}

Note, if the attribute isn't defined you'll get a run time error - you can prevent this by either doing:

if(child.hasOwnProperty("width_info"))
    trace(child.@width_info);

or just use attribute

trace(child.attribute("width_info"));
merlinc
+2  A: 

The problem is that some_xml is your root node, so you don't need to include that.

This should work:

for each (var item:XML in Environment._XMLData.child_node.child){
    trace(item.@width_info);
}
jonathanasdf