XML搜索不返回所有结果

时间:2017-12-08 11:35:41

标签: jquery html xml

很难,我设法实现了一个允许我搜索xml文件的功能。但是,它似乎只是将文件的最后<item>的数据返回到屏幕。

例如,当搜索特朗普的关键词(对不起)时,它将返回与特朗普有关的文章(这是最后一个item)。但是,还有另一个item与文件中的特朗普(第一项)有关,但未被退回。

我的假设是,这是任何简单的解决方法,但我正在努力。任何朝着正确方向的点头都会非常感激。

HTML

<form action="" class="search-form"> Search
      <br>
      <br>
      <div class="form-group has-feedback">
        <label for="search" class="sr-only">Search</label>
        <input type="text" class="form-control" name="search" id="search" placeholder="Search for current topics, e.g., Brexit"><span class="glyphicon glyphicon-search form-control-feedback"></span>
      </div>
    <br>
  </div>
  <div class="col-md-5 col-md-offset-1">
    <br>
    <div id="search_output"> Search results will appear here.</div>

的jQuery

    $(document).ready(function(){
    $('#search').on('keyup', function(){
        $.ajax({
            type: "GET",
            url: "top_news.xml",
            dataType: "xml",
            success: parseXML
        });
    });
});
function parseXML(xml){
    var searchFor = $('#search').val();
    var reg = new RegExp(searchFor, "i");
    $(xml).find('item').each(function(){
        var title = $(this).find('title').text();
        var titleSearch = title.search(reg);
        var desc = $(this).find('description').text();
        var descSearch = desc.search(reg);
        $('#search_output').empty();
        if(titleSearch > -1){
            $('#search_output').append('Found <i>'+searchFor+'<\/i> in title: '+title.replace(reg, '<b>'+searchFor+'</b>')+'<br \/>');
        }
        if(descSearch > -1){
            $('#search_output').append('Found <i>'+searchFor+'<\/i> in description: '+desc.replace(reg, '<b>'+searchFor+'</b>')+'<br \/>');
        }
    });    
}

XML

<rss version="2.0">
<channel>
    <title> Top </title>
    <description> Latest </description>
    <language>en-gb</language>
        <item>
            <title>Palestinians clash with Israeli troops in protests over Trump’s Jerusalem speech</title>
            <description>The clashes broke out ahead of a “day of rage” called by the Hamas militant group to protest U.S. recognition of Jerusalem as Israel’s capital
            </description>
        </item>
        <item>
            <title>High winds 'a recipe for explosive fire growth' in Southern California</title>        
            <description>Dry weather and merciless winds, with gusts predicted to reach the strength of a Category 1 hurricane in mountainous areas.</description>
        </item>
        <item>
            <title>Analysis | The Daily 202: Push for Al Franken’s resignation shows importance of women in Congress</title>        
            <description>Two more accusers, plus political considerations, drove a rapid shift in Democratic messaging.</description>
        </item>
        <item>
            <title>He saved 6 men at Pearl Harbor. Finally, 76 years later, he's being honored</title>        
            <description>Seventy-six years after he saved the last six survivors of the USS Arizona, Joe George will be posthumously awarded the Bronze Star Medal for Valor.</description>
        </item>
        <item>
            <title>FBI director to face lawmakers’ questions about bureau’s handling of Trump, Clinton probes</title>        
            <description>Christopher A. Wray’s testimony before the House Judiciary Committee comes as agent’s conduct is under scrutiny.</description>
        </item>
</channel>

0 个答案:

没有答案