检查 XML 值是否在 AS3 数组中

Checking if XML values are in an AS3 array

我有一些 XML 以下格式。

 <user uid="0001">
    <FirstName>John</FirstName>
    <LastName>Smith</LastName>
    <ImagePath>images/0001.jpg</ImagePath>
    <flightno>GS1234</flightno>
</user>
<user uid="0002">
    <FirstName>Luke</FirstName>
    <LastName>Dixon</LastName>
    <ImagePath>images/0002.jpg</ImagePath>
    <flightno>TD1234</flightno>
</user>
<user uid="0003">
    <FirstName>Paul</FirstName>
    <LastName>Kerr</LastName>
    <ImagePath>images/0003.jpg</ImagePath>
    <flightno>GS1234</flightno>
</user>

这是一个小样本,其中有 100 个。

我已经使用 E4x 过滤过滤掉另一组生成 as3 数组的 XML 数据。该数组包含一些航班号(例如:[GS1234,PB7367,TD1234].

我想知道如何过滤我的 XML(如上所示)以仅显示其 'flightno' 存在于 AS3 数组中的用户。

我猜它是某种 E4X 查询,但我似乎没猜对!

谢谢!

// Don't mind me using this trick with inline XML, it's not the point.
// It's here just to make it possible to copy and paste code 
// with multiline XML sample.The actual solution is a one-liner below.
var usersXML:XML = new XML(<x><![CDATA[
<data>
<user uid="0001">
    <FirstName>John</FirstName>
    <LastName>Smith</LastName>
    <ImagePath>images/0001.jpg</ImagePath>
    <flightno>GS1234567</flightno>
</user>
<user uid="0002">
    <FirstName>Luke</FirstName>
    <LastName>Dixon</LastName>
    <ImagePath>images/0002.jpg</ImagePath>
    <flightno>TD1234</flightno>
</user>
<user uid="0003">
    <FirstName>Paul</FirstName>
    <LastName>Kerr</LastName>
    <ImagePath>images/0003.jpg</ImagePath>
    <flightno>GS1234</flightno>
</user>
</data>
]]></x>.toString());

// once again, the way I create sample departingXML 
// is not important, it's just for copying and pasting into IDE.
var departingXML:XML = new XML(<x><![CDATA[
<flights>
    <flight>
        <number>GS1234</number>
        <date>10/11/2015</date>
        <time>1440</time>
    </flight>
    <flight>
        <number>TD1234</number>
        <date>10/11/2015</date>
        <time>1450</time>
    </flight>
</flights>
]]></x>.toString());


// 1. create filter array
var flightNoArray:Array = [];
departingXML.flight.number.(flightNoArray.push(toString()));

trace(flightNoArray); // GS1234,TD1234
trace(typeof(flightNoArray[0])); // string

// 2. filter users:
var list:XMLList = usersXML.user.(flightNoArray.indexOf(flightno.toString()) >= 0);

trace(list); // traces users 0002 and 0003

虽然我不会称之为高效或至少可读。

// Note: this line is somewhat queer and I don't like it,

departingXML.flight.number.(flightNoArray.push(toString()));

// but this is the only way I can now think of to get and array 
// of strings from an XMLList nodes without a loop.
// I would advise to use a readable and simple loop instead.
  • usersXML.user -- 这会得到一个 XMLList,其中所有节点都命名为 "user"
  • usersXML.user.(some condition) -- 给定条件
  • 过滤用户节点的XMLList
  • flightNoArray.indexOf(flightno.toString()) >= 0 -- 这是过滤条件
  • flightno.toString() -- 在 flightno child
  • 中得到一个字符串
  • REFERENCE: Traversing XML structures.
  • Explanation of the search trick 在上面的注释中。

更新: 结果在评论中指出,过滤器数组的填充方式也造成了麻烦。下面的代码演示了更多的 E4X。

这是过滤器列表的创建方式以及实际发生的情况:

// once again, the way I create sample departingXML 
// is just for the sake of copying and pasting, it's not related to solution.
var departingXML:XML = new XML(<x><![CDATA[
<flights>
    <flight>
        <number>GS1234</number>
        <date>10/11/2015</date>
        <time>1440</time>
    </flight>
    <flight>
        <number>TD1234</number>
        <date>10/11/2015</date>
        <time>1450</time>
    </flight>
</flights>
]]></x>.toString());

// the way it was done before
var flightNoArray: Array = []; 
for each(var num: XML in departingXML.flight) { 
    flightNoArray.push(num.number);

    // WARNING! num.number is an XMLList! It s NOT a string.
    // Addressing nodes by name ALWAYS gets you an XMLList, 
    // even if there's only one node with that name
    // Hence, `flightNoArray.indexOf("string")` could not possibly work, 
    // as there are lists inside of the array, not strings.


    // We can check if this is a fact:
    trace(flash.utils.getQualifiedClassName(flightNoArray[flightNoArray.length-1])); 
    // (traces XMLList)

    // Or this way (note toXMLString() method)
    trace(flightNoArray[flightNoArray.length-1].toXMLString()); 
    // (traces <number>GS1234</number>)
} 

trace(flightNoArray); 

trace(flightNoArray); 跟踪 GS1234,TD1234,因为这是 toString() 方法对 xml 节点的工作方式——它获取文本,即在内部。这就是为什么有一个特殊的方法 toXMLString(),它可以为您提供节点的字符串表示形式。