Access XML nodes with integer names

最后都变了- 提交于 2020-01-07 02:18:28

问题


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.

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?


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

<place3>
    ...
</place3>

...when I want

<2>
    ...
</2>

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.


回答1:


Use E4X search expression on XMLList.

trace(xml.children().(name() == "2").toXMLString());
  1. Get all children
  2. Search for the name() you need.



回答2:


From the XML specification:

[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. 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:

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



回答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. If it were me I'd fix the XML. That's going to haunt you later.




回答4:


If you want a short-term fix, change your non-XML with its non-standard tags to standard XML with proper named tags. 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.



来源:https://stackoverflow.com/questions/10398358/access-xml-nodes-with-integer-names

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!