简体   繁体   English

在ActionScript 2中递归解析XML

[英]Recursively parsing XML in Actionscript 2

I'm looking for an efficient and reusable way to parse xml into an object in actionscript2. 我正在寻找一种有效且可重用的方法来将xml解析为actionscript2中的对象。 The xml structure itself might change so it's important that im able to parse the xml with out 'hard coding' specific nodes, etc. xml结构本身可能会发生变化,因此,必须能够在不进行“硬编码”特定节点等的情况下解析xml,这一点很重要。

I normally use As3 and wouldn't need something like this since the XML class is easy to drill down into. 我通常使用As3,因为XML类很容易钻研,所以不需要这样的东西。 Below is AS3 pseudocode of what I'm trying to accomplish. 以下是我要完成的AS3伪代码。

    public function XmlObject(myXmlObject:XML,_node:String):Object
    {
        var xmlObj:Object=new Object();

        for(var node:uint=0;node<myXmlObject[_node].children().length();node++)
        {
            var attributesList:XMLList=myXmlObject[_node].children()[node].attributes();
            var nodeName:String=myXmlObject[_node].children()[node].name(); 

            switch(attributesList.length()>1)
            {
                //////////////////////
                case false:
                //////////////////////
                {
                  for each(var attribute:XML in attributesList)
                  { 
                    xmlObj[nodeName]=attribute;
                  } 
                break;


                //////////////////////
                case true:
                //////////////////////
                var values:Array=[];
                for each(attribute in attributesList)
                {
                    values.push(attribute);
                    xmlObj[nodeName][String(attribute.name())]=attribute;
                }   
                break;
            }
        }
    return xmlObj;
    }

Thanks in advance for any help on this! 在此先感谢您的任何帮助!

i did not fully understand your pseudo code ... what happens to the Array values ? 我没有完全理解您的伪代码...数组values发生了什么? seems to be simply discarded ... also, it seems not to be recursive ... 似乎只是被丢弃了...而且,它似乎不是递归的...

the problem is, that the semantics of XML and ECMA-objects is different ... 问题是,XML和ECMA对象的语义是不同的...

what would you map this to? 您会将此映射到什么?

<cart><item /><item /></cart>

and then, what would this be? 然后,这将是什么?

<cart><item /><cart>

and what this? 那这是什么?

<cart />

the problem is, that in the first case, you have an array, in the second a property, in the third nothing ... so you can't know, what cart.item will be ... even if you say, that single child nodes will be wrapped into an array, you could still have no entry, and thus cart.item is null ... not that as2 would complain if you access properties of null but still, this is quite uggly ... 问题是,在第一种情况下,您有一个数组,在第二种情况下,您有一个属性,在第三个情况中,什么都没有……所以您不知道什么是cart.item ……即使您说,单个子节点将被包装到一个数组中,您仍然可能没有条目,因此cart.itemnull ……如果您访问null属性,则as2不会抱怨,但是这样做仍然很麻烦。

e4x seems to be the best way to traverse XML objects from an ECMA world ... after a little thinking, i've put together a little (quite hacky) library: http://code.google.com/p/as24x/ you can find other libraries on google though, that support more features ... it's rather about syntax ... e4x似乎是从ECMA世界中遍历XML对象的最好方法...经过一番思考,我整理了一个(相当hacky)库: http : //code.google.com/p/as24x/您可以在Google上找到其他支持更多功能的库...而是语法...

hope this helps ;) 希望这可以帮助 ;)

greetz 格里茨

back2dos back2dos

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

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