开发者

Parsing XML with nodes containing underscores

How do I parse an XML document that conta开发者_如何学Cins 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!


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);
}


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"));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜