as3 namespace - get an attribute with a minus sign in it [duplicate]
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 nam开发者_开发知识库e="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
}
}
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']);
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.
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
精彩评论