search and output data from an XML file using JavaScript
-
I've got a set of data, which are names of places and their location which are saved in an XML file as follows: <row> <cell>name</cell> <cell>location</cell> </row> The data was extracted from a spreadsheet and then saved out into XML format. Now in the XML file there are thousands of rows, in this first instance we're looking at 5k+. Is it possible for me to search through this XML file using JavaScript, based on user input and then display this output on an HTML page? I also have to support IE6 (this is a BIG issue) I'm a total noob with regards to XML but can do a bit of JavaScript and JQuery! Is there an easier way to do this? I don't have the option of using a server side language nor can I use a database (weak I know). Thanks in advance.
-
Answer:
If you have the XML as a string then you should be able to wrap it into a jQuery object like so: var $myXML = $(myXMLString); Now you can use the jQuery methods for traversal and searching. For example, search for 'smith' in your cells: var $matches = $myXML.find("cell:contains('smith')"); //'smith' being your user input Your XML doesn't appear to have any metadata, so we can't limit the search to a particular field. If your cells had a 'fieldname' for example: <row> <cell fieldname='name'>name</cell> <cell fieldname='location'>location</cell> </row> then you could use this: var $matches = $myXML.find("cell[fieldname='name']:contains(smith)"); See an example at http://jsfiddle.net/JMeYu/ EDIT I've made this a little more sophisticated: var $myXML = $(myXMLString); var $rowMatches = $myXML.filter(function(){ var $cellMatches = $(this).find("cell:contains('smith')"); return $cellMatches.length > 0; }); alert($rowMatches.length); (Also at http://jsfiddle.net/JMeYu/1/) Now in your $rowMatches you will have the rows that match your query. The filter function contains a filter for your name. You can try turning this into an array using the $.makeArray() function, or you could iterate over the collection using the .each() function on the collection. Either way, you should be able to access the other fields in the row.
Kiz at Stack Overflow Visit the source
Related Q & A:
- how delete node in xml file using php?Best solution by Stack Overflow
- How to convert build.xml to maven pom.xml file?Best solution by Stack Overflow
- how to parse a xml file using jquery and phonegap?Best solution by Stack Overflow
- How to get the file using it's URL in Javascript?Best solution by befused.com
- How to search for a particular string in a text file using java?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.