Can't parse XML AS3 in flash with namespaces

241 views Asked by At

I don't understand why I can't parse these datas:

<places yahoo:start="0" yahoo:count="1" yahoo:total="1" xmlns="http://where.yahooapis.com/v1/schema.rng" xmlns:yahoo="http://www.yahooapis.com/v1/base.rng">

    <place yahoo:uri="http://where.yahooapis.com/v1/place/23424819" aaa:lang="en-US" xmlns:aaa="http://www.w3.org/XML/1998/namespace">
        <woeid>23424819</woeid>
        <placeTypeName code="12">Pays</placeTypeName>
        <name>France</name>
        <country type="Pays" code="FR" woeid="23424819">France</country>
        <timezone type="Fuseau Horaire" woeid="28350911">Europe/Paris</timezone>
    </place>
</places>

I want to get woeid (23424819) This is the full xml data, sorry the was an error on >places>

I tried: ...

var xml:XML = new XML(e.currentTarget.data);
trace(xml); // => that is working it is print xml datas
trace (xml.places); // => nothing
trace (xml.place); // => nothing
trace (xml.place.woeid); // => nothing
trace (xml.place[0].woeid); // => nothing

How can I get the woeid?

1

There are 1 answers

0
BadFeelingAboutThis On BEST ANSWER

Since the root places node declares a specific namespace, you need to tell AS3 to use that namespace before accessing it:

Here is an example:

//create a namespace that matches the namespace of the places node
var ns:Namespace = new Namespace("http://where.yahooapis.com/v1/schema.rng");

//tell AS3 to use this namespace as the default
default xml namespace = ns;

var xml:XML = <places yahoo:start="0" yahoo:count="1" yahoo:total="1" xmlns="http://where.yahooapis.com/v1/schema.rng" xmlns:yahoo="http://www.yahooapis.com/v1/base.rng">

    <place yahoo:uri="http://where.yahooapis.com/v1/place/23424819" xml:lang="fr">
        <woeid>23424819</woeid>
        <placeTypeName code="12">Pays</placeTypeName>
        <name>France</name>
        <country type="Pays" code="FR" woeid="23424819">France</country>
        <timezone type="Fuseau Horaire" woeid="28350911">Europe/Paris</timezone>
    </place>
</places>;

//now everything you'd expect should work. 
trace(xml.place[0].woeid);

If you have lots of namespaces, and don't want to set the default, you can also just do this:

trace(xml.ns::place.ns::woeid);

If you needed to access something in the yahoo namespace (like the uri of the place node), you could do the following:

var yNs:Namespace = new Namespace("http://www.yahooapis.com/v1/base.rng");
trace(xml.place.@yNs::uri);