如何在XML中搜索给定的字符串?

时间:2013-08-26 04:04:17

标签: xml actionscript-3 flex xml-attribute

我有一个XML:

<bodyText>
  <1 beginIndex="0" endIndex="723">
    The teddy bear is a soft toy in the form of a bear. Teddy bears are among the most popular gifts for children and are often given to adults to signify love, congratulations or sympathy.</1>

<2 beginIndex="724" endIndex="347">
    Morris Michtom saw the drawing of Roosevelt and was inspired to create a new toy. He created a little stuffed bear cub and put it in his shop window with a sign that read "Teddy's bear," after sending a bear to Roosevelt and receiving permission to use his name. 
</2>
</bodyText>

如何在XML中搜索特定字符串? 例如:如果用户输入“罗斯福的绘画”。第二个xml元素包含此字符串,它应返回2(它应返回段号no)。 怎么找出来?

1 个答案:

答案 0 :(得分:0)

将xml转换为内部类,让我们将其称为具有id的MyEntry(示例中的1和2值,但将它们作为属性放置,节点的名称无关紧要,放置“a”),beginIndex,endIndex和值(你的字符串)并在MyEntry中放入一个方法,如:

public function search( string:String ):Boolean
{
    return value.indexOf( string ) != -1;
}

//When you search for the string "drawing of Roosevelt":
for each( var entry:MyEntry in entries )
{
    if( entry.search( string ) )
    {
       trace( "Found!" );
       break;
    }
}
相关问题