Click to See Complete Forum and Search --> : How to search within "XML Files" using "Javascript"?


adithy
02-17-2010, 12:00 AM
Hi All,

I have a folder full of almost 300 XML files. I want to search within these files.I don't want to have to open all 300 files one by one and do a find for the text i'm looking for in them. Can anyone give me sample "Javascript" code that searches within "XML" files.

Charles
02-17-2010, 10:57 AM
What platform are you using?

adithy
02-17-2010, 10:42 PM
Hi Charles, am using Javascript?

Charles
02-18-2010, 05:35 AM
Javascript on what platform? Is this a Windows box? Is this an HTML page or HTA or are you using the Windows Scripting Host?

If this is just a one time deal and you're using Windows, then yes, this is quite possible and rather simple to do in JavaScript.

Charles

adithy
02-18-2010, 09:32 AM
Currently working on windows platform, but i need it later on mac.

I used follwing code, It works well for HTML pages. But it won't work for XML pages.


/* http://www.kryogenix.org/code/browser/searchhi/ */
/* Modified 20021006 to fix query string parsing and add case insensitivity */
/* Modified 20070316 to stop highlighting inside nosearchhi nodes */
/* Modified 20081217 to do in-page searching and wrap up in an object */
/* Modified 20081218 to scroll to first hit like
http://www.woolyss.free.fr/js/searchhi_Woolyss.js and say when not found */

searchhi = {
highlightWord: function(node,word) {
// Iterate into this nodes childNodes
if (node.hasChildNodes) {
for (var hi_cn=0;hi_cn<node.childNodes.length;hi_cn++) {
searchhi.highlightWord(node.childNodes[hi_cn],word);
}
}

// And do this node itself
if (node.nodeType == 3) { // text node
tempNodeVal = node.nodeValue.toLowerCase();
tempWordVal = word.toLowerCase();
if (tempNodeVal.indexOf(tempWordVal) != -1) {
var pn = node.parentNode;
// check if we're inside a "nosearchhi" zone
var checkn = pn;
while (checkn.nodeType != 9 &&
checkn.nodeName.toLowerCase() != 'body') {
// 9 = top of doc
if (checkn.className.match(/\bnosearchhi\b/)) { return; }
checkn = checkn.parentNode;
}
if (pn.className != "searchword") {
// word has not already been highlighted!
var nv = node.nodeValue;
var ni = tempNodeVal.indexOf(tempWordVal);
// Create a load of replacement nodes
var before = document.createTextNode(nv.substr(0,ni));
var docWordVal = nv.substr(ni,word.length);
var after = document.createTextNode(nv.substr(ni+word.length));
var hiwordtext = document.createTextNode(docWordVal);
var hiword = document.createElement("span");
hiword.className = "searchword";
hiword.appendChild(hiwordtext);
pn.insertBefore(before,node);
pn.insertBefore(hiword,node);
pn.insertBefore(after,node);
pn.removeChild(node);
searchhi.found += 1;
if (searchhi.found == 1) pn.scrollIntoView();
}
}
}
},

googleSearchHighlight: function() {
var ref = document.referrer;
if (ref.indexOf('?') == -1) return;
var qs = ref.substr(ref.indexOf('?')+1);
var qsa = qs.split('&');
for (var i=0;i<qsa.length;i++) {
var qsip = qsa[i].split('=');
if (qsip.length == 1) continue;
if (qsip[0] == 'q' || qsip[0] == 'p') { // q= for Google, p= for Yahoo
var wordstring = unescape(qsip[1].replace(/\+/g,' '));
searchhi.process(wordstring);
}
}
},

process: function(wordstring) {
searchhi.found = 0;
var words = wordstring.split(/\s+/);
for (w=0;w<words.length;w++) {
searchhi.highlightWord(document.getElementsByTagName("body")[0],words[w]);
}
if (searchhi.found === 0) {
searchhi.nohits();
}
},

nohits: function() {
},

init: function() {
if (!document.createElement || !document.getElementsByTagName) return;
// hook up forms of type searchhi
var frms = document.getElementsByTagName("form");
for (var i=0; i<frms.length; i++) {
if (frms[i].className.match(/\bsearchhi\b/)) {
frms[i].onsubmit = function() {
var inps = this.getElementsByTagName("input");
for (var j=0; j<inps.length; j++) {
if (inps[j].type == "text") {
searchhi.process(inps[j].value);
return false;
}
}
};
}
}
// highlight search engine referrer results
searchhi.googleSearchHighlight();
}
};

(function(i) {var u =navigator.userAgent;var e=/*@cc_on!@*/false; var st =
setTimeout;if(/webkit/i.test(u)){st(function(){var dr=document.readyState;
if(dr=="loaded"||dr=="complete"){i()}else{st(arguments.callee,10);}},10);}
else if((/mozilla/i.test(u)&&!/(compati)/.test(u)) || (/opera/i.test(u))){
document.addEventListener("DOMContentLoaded",i,false); } else if(e){ (
function(){var t=document.createElement('doc:rdy');try{t.doScroll('left');
i();t=null;}catch(e){st(arguments.callee,0);}})();}else{window.onload=i;}})(searchhi.init);


Can you please correct the code? I couldn't find the solution.

Charles
02-18-2010, 11:31 AM
IF it needs to work on a Mac then I can't help you. Perhaps try Perl or PHP. JavaScript doesn't normally have file controls but it does under Windows if you can get the security dropped.

rnd me
02-21-2010, 07:30 AM
use ajax to load the xml file.
i don't know ecatly how precise you need to search, but you can simply example the responseText of the ajax call to find matches.

as long as your html page is in the same folder as your xml files, ajax should run fine using relative paths.
without some type of index file, you will need to open all 300 files to search all the files.

you can cache all the files in javascript, so the 2nd search couold be much faster, but it should still take a min or two to peek at 300 files.

adithy
03-09-2010, 08:54 AM
Hi Friends, i have found the solution for that problem.
But this code highlighting all searched terms at the same time. This should not happen for me, instead i should use two buttons , one is "Previous" and the other one is "Next". How can i modify the code for that? The loop should get exit each time, once it highlighted the matched term. Please help me to solve this Problem!
/*
* This is the function that actually highlights a text string by
* adding HTML tags before and after all occurrences of the search
* term. You can pass your own tags if you'd like, or if the
* highlightStartTag or highlightEndTag parameters are omitted or
* are empty strings then the default <font> tags will be used.
*/
var SearchResultCount = 0;
function doHighlight(bodyText, searchTerm, highlightStartTag, highlightEndTag)
{
// the highlightStartTag and highlightEndTag parameters are optional
if ((!highlightStartTag) || (!highlightEndTag)) {
highlightStartTag = "<span name= 'highlighted_text' style='color:blue; background-color:yellow;'>";
highlightEndTag = "</span>";
}

// find all occurences of the search term in the given text,
// and add some "highlight" tags to them (we're not using a
// regular expression search, because we want to filter out
// matches that occur within HTML tags and script blocks, so
// we have to do a little extra validation)
var newText = "";
var i = -1;
var lcSearchTerm = searchTerm.toLowerCase();
var lcBodyText = bodyText.toLowerCase();

while (bodyText.length > 0) {
i = lcBodyText.indexOf(lcSearchTerm, i+1);
if (i < 0) {
newText += bodyText;
bodyText = "";
} else {
// skip anything inside an HTML tag
if (bodyText.lastIndexOf(">", i) >= bodyText.lastIndexOf("<", i)) {
// skip anything inside a <script> block
if (lcBodyText.lastIndexOf("/script>", i) >= lcBodyText.lastIndexOf("<script", i)) {
newText += bodyText.substring(0, i) + highlightStartTag + bodyText.substr(i, searchTerm.length) + highlightEndTag;
bodyText = bodyText.substr(i + searchTerm.length);
lcBodyText = bodyText.toLowerCase();
i = -1;
SearchResultCount++;
}
}
}
}
return newText;
}


/*
* This is sort of a wrapper function to the doHighlight function.
* It takes the searchText that you pass, optionally splits it into
* separate words, and transforms the text on the current web page.
* Only the "searchText" parameter is required; all other parameters
* are optional and can be omitted.
*/
function highlightSearchTerms(searchText, treatAsPhrase, warnOnFailure, highlightStartTag, highlightEndTag)
{
// if the treatAsPhrase parameter is true, then we should search for
// the entire phrase that was entered; otherwise, we will split the
// search string so that each word is searched for and highlighted
// individually
if (treatAsPhrase) {
searchArray = [searchText];
} else {
searchArray = searchText.split(" ");
}
var bodyText = document.body.innerHTML;
for (var i = 0; i < searchArray.length; i++) {
bodyText = doHighlight(bodyText, searchArray[i], highlightStartTag, highlightEndTag);
}
document.body.innerHTML = bodyText;
return true;
}


/*
* This displays a dialog box that allows a user to enter their own
* search terms to highlight on the page, and then passes the search
* text or phrase to the highlightSearchTerms function. All parameters
* are optional.
*/

function searchPrompt(defaultText, treatAsPhrase, textColor, bgColor)
{
// This function prompts the user for any words that should
// be highlighted on this web page
if (!defaultText) {
defaultText = "";
}

// we can optionally use our own highlight tag values
if ((!textColor) || (!bgColor)) {
highlightStartTag = "";
highlightEndTag = "";
}
else {
highlightStartTag = "<span name= 'highlighted_text' style='color:" + textColor + "; background-color:" + bgColor + ";'>";
highlightEndTag = "</span>";
}

/* if (treatAsPhrase) {
promptText = "Please enter the phrase you'd like to search for:";
} else {
promptText = "Please enter the words you'd like to search for, separated by spaces:";
}
*/
searchText = defaultText;

if (!searchText) {
// alert("No search terms were entered. Exiting function.");
return false;
}
return highlightSearchTerms(searchText, treatAsPhrase, true, highlightStartTag, highlightEndTag);
}

function MyApp_RemoveAllHighlightsXml() {
var remove=document.getElementsByTagName("span");
if (remove) {
for (var i=remove.length-1;i>-1;i--) {
if (remove[i].getAttribute("name")=="highlighted_text") {
var s=remove[i].innerHTML;
var otxt=document.createTextNode(s)
remove[i].parentNode.insertBefore(otxt,remove[i]);
remove[i].parentNode.removeChild (remove[i]);
}
}
}
}