as3 namespace - get an attribute with a minus sign in it [duplicate]

旧城冷巷雨未停 提交于 2019-12-23 20:07:28

问题


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

I've set the namespace for my XML to use SMIL and I'm able to pull the src attribute of an element this way:

my.node.@src which gets "this is some URL"

However, I have another attr called 'system-bitrate'. Because of the minus sign, I can't do @system-bitrate So I attempted what I normally do which is my.node.attribute('system-bitrate') which isn't working.

Oddly enough, not even my.node.attribute('src') works. I suspect this is due to the namespace? So how to I get attributes out using ny.node.attribute ?

The only thing that works is my.node.attributes()[1]. I know that's not the "right way" so I'm hoping someone can enlighten me!

FYI I'm working with SMIL files

** edit **

Here's the namespace required for the XML I'm using: default xml namespace = new Namespace('http://www.w3.org/2001/SMIL20/Language');

And an example of the XML I'm working with:

<smil xmlns="http://www.w3.org/2001/SMIL20/Language">
  <head>
    <meta name="title" content="Live"/>
  </head>
  <body>
    <switch>
      <video src="myStreamName" system-bitrate="200000"/>
    </switch>
  </body>
</smil>

** code sample for DennisJaaman **

default xml namespace = new Namespace('http://www.w3.org/2001/SMIL20/Language');

var xml:XML = XML(event.target.data);
for each(var o:XML in xml.body['switch'].video) {
    if(!hs) hs = o;
    else {
        trace(o.attributes()[1]); // works
                trace(o.@url); // doesn't work either (makes me wonder about NS issues
                trace(o['@system-bitrate']); // doesn't work
                trace(o.attribute('@system-bitrate') // doesn't work
                // etc etc, I just left a few in here
    }
}

回答1:


Try to use square brackets like in the sample below:

default xml namespace = new Namespace("http://www.w3.org/2001/SMIL20/Language");
var xmlSmpl:XML=<smil xmlns="http://www.w3.org/2001/SMIL20/Language">
  <head>
    <meta name="title" content="Live"/>
  </head>
  <body>
    <switch>
      <video src="myStreamName" system-bitrate="200000"/>
    </switch>
  </body>
</smil>;

trace (xmlSmpl.body['switch']['video']['@system-bitrate']);



回答2:


Behold! The power of QName!

my.node.attribute( 
    new QName( 'http://www.w3.org/2001/SMIL20/Language', 'system-bitrate' ) 
)

The thing about attribute (and descendant, and child...) is that its parameter is type * (anonymously). This is because it really isn't a String, it is coerced to a QName (without a URI) in the background. This means you were searching under the default URI for something under the URI above.

Let me know how that code above works out.




回答3:


Check out this post:

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

******EDIT****:

And use the following notation to get XML Attributes that contain a - (dash)

trace("Video system-bitrate: " + my.node.video["@system-bitrate"]);

These do not work:

trace("Video system-bitrate: " + my.node.video.@["system-bitrate"]);
trace("Video system-bitrate: " + my.node.video.attribute("@system-bitrate"));

For more info check the LiveDocs

Cheers



来源:https://stackoverflow.com/questions/6656469/as3-namespace-get-an-attribute-with-a-minus-sign-in-it

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