在一个XML文件中选择特定的数据文件、数据、XML

2023-09-09 21:40:15 作者:浪浪浪味仙女″

我有这个xml文件

<data-set xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <record>
        <Date>1 July</Date>
        <Ville>New-York</Ville>
        <haute>6h50</haute>
        <basse>1h14</basse>
        <haute2>20h01</haute2>
        <basse2>13h16</basse2>
    </record>
    <record>
        <Date>2 July</Date>
        <Ville>New-York</Ville>
        <haute>7h36</haute>
        <basse>1h59</basse>
        <haute2>20h41</haute2>
        <basse2>13h56</basse2>
    </record>
    <record>
        <Date>3 July</Date>
        <Ville>Miami</Ville>
        <haute>8h21</haute>
        <basse>2h44</basse>
        <haute2>21h22</haute2>
        <basse2>14h37</basse2>
    </record>
</data-set>

我想,在我的AS3 code,才能够从这个XML文件中选择特定的数据。

I would like, in my AS3 code, to be able to select specific data from this xml file.

例如:

var currentDate=new Date();
var day=currentDate.getDate();

If the day == one of the day of the xml, display "haute" of the corresponding day.

这可能吗?我该怎么办呢?

Is it possible ? How can I do that ?

推荐答案

您可以做你正在寻找通过过滤由属性或元素的XML数据,了解更多详情什么看一看此处。

You can do what you are looking by filtering your xml data by attribute or element, for more details take a look here.

因此​​,对于你的例子,你可以这样做:

So for you example, you can do like this :

var xml:XML = 
    <data-set xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <record>
            <Date>1 July</Date>
            <Ville>New-York</Ville>
            <haute>6h50</haute>
            <basse>1h14</basse>
            <haute2>20h01</haute2>
            <basse2>13h16</basse2>
        </record>
        <record>
            <Date>2 July</Date>
            <Ville>New-York</Ville>
            <haute>7h36</haute>
            <basse>1h59</basse>
            <haute2>20h41</haute2>
            <basse2>13h56</basse2>
        </record>
        <record>
            <Date>3 July</Date>
            <Ville>Miami</Ville>
            <haute>8h21</haute>
            <basse>2h44</basse>
            <haute2>21h22</haute2>
            <basse2>14h37</basse2>
        </record>
    </data-set>
;

var search:XMLList = xml.record.(Date == '3 July');
if(search){
    trace(search.haute);    // gives : 8h21
}

和从外部文件加载数据时:

And when loading data from an external file :

var loader:URLLoader = new URLLoader(); 
    loader.addEventListener(Event.COMPLETE, on_xml_loaded);
    loader.load(new URLRequest('xml.xml'));

function on_xml_loaded(e:Event){
    var xml:XML = new XML(e.target.data);
    var search:XMLList = xml.record.(Date == '3 July');
    if(search){
        trace(search.haute);    // gives : 8h21
    }
}

希望能有所帮助。

Hope that can help.