搜索XML文件并使用javascript显示结果 [英] Search an XML file and display results with javascript

查看:32
本文介绍了搜索XML文件并使用javascript显示结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去3个小时我一直在各处搜索,但找不到任何可以帮助我的东西.非常抱歉,如果有任何答案,但我无法解决.

所以我有这个xml文件,例如:

 < entry>< title>标题</title><说明>此处说明</description></entry> 

所以我想做的是在其中包含一个带有搜索表单的html,然后根据搜索词在同一页面(例如空div)中显示与该词匹配的所有结果./p>

有可能吗?

非常感谢任何可以帮助我的人!

解决方案

我对此很好奇,没有人回答,所以我尝试了一些事情,并找出了使用jQuery的最佳简便方法

test.xml

 < item>< entry>< title>标题</title><说明>此处说明</description></entry>< entry>< title>标题2</title><说明>这里的说明第二</说明></entry></item> 

index.html

 <!DOCTYPE html>< html>< head>< script src ="http://code.jquery.com/jquery-latest.js"></script></head><身体>< input type ="text" id ="search" autocomplete ="off"/>< p id ="output"></p>< script type ="text/javascript">$(document).ready(function(){$('#search').on('keyup',function(){$ .ajax({类型:"GET",网址:"test.xml",dataType:"xml",成功:parseXML});});});函数parseXML(xml){var searchFor = $('#search').val();var reg = new RegExp(searchFor,"i");$(xml).find('entry').each(function(){var title = $(this).find('title').text();var titleSearch = title.search(reg);var desc = $(this).find('description').text();var descSearch = desc.search(reg);$('#output').empty();if(titleSearch> -1){$('#output').append('找到< i>'+ searchFor +'< \/i>标题:'+ title.replace(reg,'< b>'++ searchFor +'</b>')+'< br \/>');}if(descSearch> -1){$('#output').append('在描述中找到< i>'+ searchFor +'< \/i> ;:'+ desc.replace(reg,'< b>'++ searchFor +'</b>')+'< br \/>');}});}</script></body></html> 

I have been searching everywhere for the last 3 hours and I couldn't find anything that could help me. I'm very sorry if there are any answer around but I just couldn't get to it.

So I have this xml file eg:

<entry>
    <title>The Title</title>
    <description>Description here</description>
</entry>

So the thing I'd love to do is to have an html with a search form in it and then based on the search term display all the results within the same page (empty div for example) that matches that term.

Would that be possible?

MANY thanks in advance for anyone that can help me on this!

解决方案

I was curios about this, and none answered so I tried some things and tuns out best and easy way to do this is with jQuery

test.xml

<item>
    <entry>
        <title>The Title</title>
        <description>Description here</description>
    </entry>

    <entry>
        <title>The Title 2 </title>
        <description>Description here second</description>
    </entry>
</item>

index.html

<!DOCTYPE html>
<html>
<head>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<input type="text" id="search"  autocomplete="off" />
<p id="output"></p>

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

这篇关于搜索XML文件并使用javascript显示结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆