我对此感到好奇,但没有人回答,所以我尝试了一些事情并找到了最好和最简单的方法是使用 jQuery
测试.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>
索引.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>