简体   繁体   English

使用整数名称访问XML节点

[英]Access XML nodes with integer names

For my application, I make an HTTPRequest, and get back some XML served from a JSP. 对于我的应用程序,我创建一个HTTPRequest,并从JSP获取一些XML。 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. 该XML有一些(是的,我知道这是无效/不正确的XML。如果找不到可取的内容,我会尝试在内部寻址)以整数作为名称的节点,例如说<2>

When I attempt to access it, using myXMLVariable.child("2") , it returns the third (index=2) XML node instead. 当我尝试使用myXMLVariable.child("2")访问它时,它将返回第三个(index = 2)XML节点。 I understand that this behavior is "correct" . 我了解此行为是“正确的” Is there any way to get around this behavior? 有什么办法可以解决此问题?


Example

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") returns xml.child("2")返回

<place3>
    ...
</place3>

...when I want ...当我想要

<2>
    ...
</2>

NOTE 注意

I am aware this is invalid XML. 我知道这是无效的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. 发行日期即将到来,此解决方法将被删除,并使用适用于下一个版本的XML代替。

Use E4X search expression on XMLList. 在XMLList上使用E4X搜索表达式。

trace(xml.children().(name() == "2").toXMLString());
  1. Get all children 让所有孩子
  2. Search for the name() you need. 搜索您需要的name()。

From the XML specification: 根据XML规范:

[Definition: A Name is an Nmtoken with a restricted set of initial characters.] 
Disallowed initial characters for Names include digits, diacritics, the full stop and the hyphen.

Your <2> tag does not have a valid name. 您的<2>标记没有有效的名称。 You should not be surprised it doesn't work as expected. 您应该不会感到惊讶,它没有按预期工作。

EDIT 编辑

If there is no way to get around working with invalid documents like this, I would probably use a RegExp to replace the invalid tags with valid ones, prior to processing the result: 如果无法解决这样的无效文档,在处理结果之前,我可能会使用RegExp将无效标签替换为有效标签:

public function replaceNumericalXMLTagNames( input:String ):String {
    var reg:RegExp = /(\<\/?)([0-9]+)(\>)/g;
    return input.replace( reg, function():String {
        return arguments[1]+"num"+arguments[2]+arguments[3];
    } ) );
}

I think actionscript is 'helping' you. 我认为动作脚本正在“帮助”您。 The param for .child is an object and I'll bet that actionscript sees a number and converts it and uses it as an index. .child的参数是一个对象,我敢打赌,动作脚本会看到一个数字并将其转换并将其用作索引。 If it were me I'd fix the XML. 如果是我,我将修复XML。 That's going to haunt you later. 以后会困扰你的。

If you want a short-term fix, change your non-XML with its non-standard tags to standard XML with proper named tags. 如果要进行短期修复,请将具有非标准标记的非XML更改为具有适当命名标记的标准XML。 Then you'll be able to use standard XML tools to manipulate it, and you'll get your code working far faster as a result. 然后,您将能够使用标准XML工具对其进行操作,从而使您的代码运行得更快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM