Search XML file and display results using javascript

I have searched everywhere for the past 3 hours, and I could not find anything that could help me. I am very sorry if there is any answer, but I just could not get to it.

So, I have this xml file, for example:

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

So, I would like it to have html with a search form, and then, based on the search query, all results on one page (for example, an empty div) that matches this term are displayed.

Is it possible?

MANY in advance in advance for someone who can help me!

+2
source share
2 answers

I bought this and no one answered, so I tried some things and set up the best and easiest way to do it 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> 
+7
source

Until yesterday, this worked fine for me, but suddenly it stopped working ... Is there anything changed in the script source, http://code.jquery.com/jquery-latest.js Please answer

-1
source

Source: https://habr.com/ru/post/946917/


All Articles