e4x / as3: How to access a node with a dash in its name

丶灬走出姿态 提交于 2019-11-30 22:08:17

问题


the e4x implementation in as3 doesn't seem to be able to handle node names that have dashes in them. The musicbrainz api returns xml with a node named artist-list and i can't seem to get it to let me access the node.

sample from http://musicbrainz.org/ws/1/artist/?type=xml&name=dr%20dog :

<metadata xmlns="http://musicbrainz.org/ns/mmd-1.0#" xmlns:ext="http://musicbrainz.org/ns/ext-1.0#">
    <artist-list offset="0" count="1090">
        <artist type="Group" id="e9aed5e5-ed35-4244-872e-194862290295" ext:score="100">
        </artist>
    </artist-list>
</metadata>

If I try to access it like so myXml.artist-list i get the compile time error:

Error: Access of undefined property list.

Anybody know of a workaround?

--Edit: full source--

var l:URLLoader = new URLLoader();
    l.load(new URLRequest("http://musicbrainz.org/ws/1/artist/?type=xml&name=dr%20dog"));
    l.addEventListener(Event.COMPLETE, function(e:Event) {
        var myXml:XML = XML(e.target.data);
        trace(myXml.artist-list)
    });

回答1:


Added a working sample using the two syntaxes : http://wonderfl.net/c/hyuG

You can use myXml["my field"] notation to get your field, as your xml have namespace in it you have to specify it, one way to do this is:

var ns:Namespace=new Namespace("http://musicbrainz.org/ns/mmd-1.0#")
trace(myXml.ns::["artist-list"])

another way is to set the default namespace:

var ns:Namespace=new Namespace("http://musicbrainz.org/ns/mmd-1.0#")
default xml namespace=ns
trace(xml["artist-list"])



回答2:


You can access it with 'child'

xml.child("artist-list")

Will return an XMLList. Not as neat as regular e4x but that's the way it goes..



来源:https://stackoverflow.com/questions/5317404/e4x-as3-how-to-access-a-node-with-a-dash-in-its-name

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