使用整数名称访问 XML 节点 [英] Access XML nodes with integer names
问题描述
对于我的应用程序,我发出一个 HTTPRequest,并从 JSP 获取一些 XML.XML 有一些(是的,我知道这是无效/不正确的 XML.如果我找不到创可贴,我将尝试在内部解决该问题)以整数作为名称的节点,例如 <2>
例如.
For my application, I make an HTTPRequest, and get back some XML served from a JSP. That XML has some (yes, I'm aware this is invalid/improper XML. If I can't find a bandaid, I will try to address that internally) nodes with integers as names, say <2>
for example.
当我尝试使用 myXMLVariable.child("2")
访问它时,它返回第三个 (index=2) XML 节点.我知道这种行为是 "正确".有什么办法可以避免这种行为吗?
When I attempt to access it, using myXMLVariable.child("2")
, it returns the third (index=2) XML node instead. I understand that this behavior is "correct". Is there any way to get around this behavior?
var myXML:String = "<response>" +
"<place1>" +
" <item>1</item>" +
" <stuff>1</stuff>" +
"</place1>" +
"<2>" +
" <item>1</item>" +
" <stuff>1</stuff>" +
"</2>" +
"<place3>" +
" <item>1</item>" +
" <stuff>1</stuff>" +
"</place3>" +
"</response>";
protected function getParam():void
{
var xml:XML = new XML(myXML);
Alert.show(xml.child("2"));
//trace(xml.child("2"))
}
xml.child("2")
返回
<place3>
...
</place3>
...当我想要时
<2>
...
</2>
注意
我知道这是无效的 XML.我正在寻找一种解决方法,即短期修复.有一个近期的发布日期,此变通方法将被删除并替换为下一个版本的适当 XML.
NOTE
I am aware this is invalid XML. I am looking for a workaround, a short term fix. There is a near-future release date, and this workaround will be removed and replaced with proper XML for the next version.
推荐答案
在 XMLList 上使用 E4X 搜索表达式.
Use E4X search expression on XMLList.
trace(xml.children().(name() == "2").toXMLString());
- 获取所有孩子
- 搜索您需要的 name().
这篇关于使用整数名称访问 XML 节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!