Busque un archivo XML y muestre los resultados con javascript

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:

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

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.

¿Sería eso posible?

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

preguntado el 25 de agosto de 12 a las 00:08

1 Respuestas

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


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

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


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

<script type="text/javascript">
    $('#search').on('keyup', function(){
            type: "GET",
            url: "test.xml",
            dataType: "xml",
            success: parseXML
function parseXML(xml){
    var searchFor = $('#search').val();
    var reg = new RegExp(searchFor, "i");
        var title = $(this).find('title').text();
        var titleSearch = title.search(reg);
        var desc = $(this).find('description').text();
        var descSearch = desc.search(reg);
        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 \/>');

Respondido 25 ago 12, 02:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.