0

我有一个像这样的 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(它应该返回段落号)。如何找出?

4

1 回答 1

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;
    }
}
于 2013-08-26T08:08:45.443 回答