e4x/as3:如何访问名称中带有破折号的节点 [英] e4x / as3: How to access a node with a dash in its name

查看:21
本文介绍了e4x/as3:如何访问名称中带有破折号的节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

as3 中的 e4x 实现似乎无法处理包含破折号的节点名称.musicbrainz api 返回带有名为 artist-list 的节点的 xml,我似乎无法让它让我访问该节点.

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.

样本来自 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>

如果我尝试像这样访问它 myXml.artist-list 我得到编译时错误:

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

Error: Access of undefined property list.

有人知道解决方法吗?

--完整源代码--

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

推荐答案

使用两种语法添加了一个工作示例:http://wonderfl.net/c/hyuG

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

您可以使用 myXml["my field"] 符号来获取您的字段,因为您的 xml 中有 namespace 您必须指定它,一种方法这是:

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"])

这篇关于e4x/as3:如何访问名称中带有破折号的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆