繁体   English   中英

无法解析带有命名空间的 Flash 中的 XML AS3

[英]Can't parse XML AS3 in flash with namespaces

我不明白为什么我无法解析这些数据:

<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>

我想得到 woeid (23424819) 这是完整的 xml 数据,抱歉,在 >places> 上有错误

我试过: ...

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

我怎样才能得到woeid

由于根节点声明了一个特定的命名空间,因此您需要在访问它之前告诉 AS3 使用该命名空间:

下面是一个例子:

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

如果你有很多命名空间,并且不想设置默认值,你也可以这样做:

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

如果您需要访问yahoo命名空间中的某些内容(例如地点节点的 uri),您可以执行以下操作:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM