Go Back   CodingForums.com > :: Client side development > XML

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-13-2009, 03:40 AM   PM User | #1
domy
New to the CF scene

 
Join Date: Aug 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
domy is an unknown quantity at this point
Smile <<help select search xml source with dropdown>> code inside!

Hello guys! here is a search code that was working fine until now.. =( what im trying to do is to select the xml source to be searched using a dropdown.. but now i dont know what's wrong.. just take a look, and let me know if you get it right.. thxx! =)

note: the problem must be on loading the xml and/or dropdown select,, not on the search code...




<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<title>Search XML</title>

<script type="text/javascript">
window.onload = loadIndex;
function loadIndex() { // load indexfile
// most current browsers support document.implementation
if (document.implementation && document.implementation.createDocument) {
var docdoc = document.getElementById('urlfile').value;
xmlDoc = document.implementation.createDocument("", "", null);
xmlDoc.load(docdoc);
}
// MSIE uses ActiveX
else if (window.ActiveXObject) {
var docdoc = document.getElementById('urlfile').value;
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
xmlDoc.load(docdoc);
}
}

function searchIndex() { // search the index (duh!)
if (!xmlDoc) {
loadIndex();
}
// get the search term from a form field with id 'searchme'
var searchterm = document.getElementById("searchme").value;


var allitems = xmlDoc.getElementsByTagName("item");
results = new Array;

if (searchterm.length < 2) {
alert("Please enter at least 2 digits");
}

else {
for (var i=0;i<allitems.length;i++) {

// see if the XML entry matches the search term,
// and (if so) store it in an array

var name = allitems[i].lastChild.nodeValue;
var exp = new RegExp(searchterm,"i");
if ( name.match(exp)!= null) {
results.push(allitems[i]);
}

}

// send the results to another function that displays them to the user
showResults(results, searchterm);
}
}


// Write search results to a table
function showResults(results, searchterm) {

if (results.length > 0) {
// if there are any results, write them to a table

document.write('<div><a href="searchxml.htm">New Search</a></div>You searched for <b><i>'+searchterm+'</i></b><br><br>');
document.write('<table border-top="3px" style="width: 100%; background-color: #cccccc; font-color:#ffffff; border-color:black;">');
document.write('<tr><th>NAME</th><th>AGE</th><th>HEIGHT</th><th>WEIGHT</th></tr>');
for(var i=0; i<results.length; i++) {

document.write('<tr >');
document.write('<td>' + results[i].getAttribute("name") + '<br />');
document.write('<img src="' + results[i].getAttribute("image") + '"></td>');
document.write('<td >' + results[i].getAttribute("age") + '</td>');
document.write('<td>' + results[i].getAttribute("height") + '</td>');
document.write('<td>' + results[i].getAttribute("weight") + '</td>');
document.write('</tr>');

}

document.write('<table>');
document.close();


} else {
// else tell the user no matches were found
var notfound = alert('No results found for '+searchterm+'!');
}
}

}
</script>

</head>
<body >

<select id="urlfile" onchange="loadIndex()">
<option value="index.xml"> XML 1</option>
<option value="index2.xml"> XML 2</option>
</select>

<form action=""><b>Search:&nbsp;&nbsp;</b>
Name<input id="searchme" type="text" size="20">&nbsp;&nbsp;
<input value="Submit" onclick="searchIndex(); return false;" type="submit">
</form>

</body>
</html>







index.xml


<?xml version="1.0" encoding="utf-8"?>
<searchable_index>
<item name="John" image="profile.png" age="22" height="5 ft 5 inches" weight="150">John</item>
<item name="Paul" image="profile2.png" age="25" height="5 ft 9 inches" weight="168">Paul</item>
<item name="George" image="profile3.png" age="27" height="6 ft 1 inches" weight="175">george</item>
<item name="Ringo" image="profile4.png" age="23" height="6 ft 3 inches" weight="180">ringo</item>

</searchable_index>



index2.xml


<?xml version="1.0" encoding="utf-8"?>
<searchable_index>
<item name="James" image="profile.png" age="22" height="5 ft 5 inches" weight="150">James</item>
<item name="Patrick" image="profile2.png" age="25" height="5 ft 9 inches" weight="168">Patrick</item>
<item name="Carl" image="profile3.png" age="27" height="6 ft 1 inches" weight="175">Carl</item>
<item name="Ronny" image="profile4.png" age="23" height="6 ft 3 inches" weight="180">Ronny</item>

</searchable_index>

Last edited by domy; 08-13-2009 at 03:47 AM..
domy is offline   Reply With Quote
Old 08-13-2009, 03:48 AM   PM User | #2
ckeyrouz
Senior Coder

 
ckeyrouz's Avatar
 
Join Date: Jun 2009
Location: Montreal, Canada
Posts: 1,044
Thanks: 5
Thanked 179 Times in 179 Posts
ckeyrouz is on a distinguished road
add async = false between these two lines:
Code:
xmlDoc = document.implementation.createDocument("", "", null);
xmlDoc.load(docdoc);
so they become:
Code:
xmlDoc = document.implementation.createDocument("", "", null);
xmlDoc.async = false;
xmlDoc.load(docdoc);
ckeyrouz is offline   Reply With Quote
Old 08-13-2009, 04:01 AM   PM User | #3
domy
New to the CF scene

 
Join Date: Aug 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
domy is an unknown quantity at this point
yep I forgot that, but still not working..

Last edited by domy; 08-13-2009 at 04:05 AM..
domy is offline   Reply With Quote
Old 08-13-2009, 06:54 AM   PM User | #4
ckeyrouz
Senior Coder

 
ckeyrouz's Avatar
 
Join Date: Jun 2009
Location: Montreal, Canada
Posts: 1,044
Thanks: 5
Thanked 179 Times in 179 Posts
ckeyrouz is on a distinguished road
Are you sure that the path of the xml files is correct.
I mean are they in the same folder where the html file is?
ckeyrouz is offline   Reply With Quote
Old 08-14-2009, 01:43 AM   PM User | #5
domy
New to the CF scene

 
Join Date: Aug 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
domy is an unknown quantity at this point
they are in the same folder, heres the test link..

http://poligoma.webs.com/XML/search.htm
domy is offline   Reply With Quote
Old 08-14-2009, 03:52 AM   PM User | #6
ckeyrouz
Senior Coder

 
ckeyrouz's Avatar
 
Join Date: Jun 2009
Location: Montreal, Canada
Posts: 1,044
Thanks: 5
Thanked 179 Times in 179 Posts
ckeyrouz is on a distinguished road
Both of your xmls are not well formed check what's happening there or post them here to see what's the problem:
http://poligoma.webs.com/XML/index.xml

this is the error:
Code:
XML Parsing Error: XML or text declaration not at start of entity
Location: http://poligoma.webs.com/XML/index.xml
Line Number 3, Column 1:<?xml version="1.0" encoding="utf-8"?>
^
ckeyrouz is offline   Reply With Quote
Old 08-14-2009, 11:52 AM   PM User | #7
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
Quote:
Originally Posted by ckeyrouz View Post
Both of your xmls are not well formed check what's happening there or post them here to see what's the problem:
http://poligoma.webs.com/XML/index.xml

this is the error:
Code:
XML Parsing Error: XML or text declaration not at start of entity
Location: http://poligoma.webs.com/XML/index.xml
Line Number 3, Column 1:<?xml version="1.0" encoding="utf-8"?>
^
it must only remove whitespaces and newlines before xml prolog, which must start in first column of the first row, in rest is well formated I guess.

best regards
oesxyl is offline   Reply With Quote
Old 08-15-2009, 01:04 AM   PM User | #8
domy
New to the CF scene

 
Join Date: Aug 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
domy is an unknown quantity at this point
the xml doesnt give me any errors, I fixed it for the test link..
domy is offline   Reply With Quote
Reply

Bookmarks

Tags
dropdown, search, select, source, xml

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:42 PM.


Advertisement
Log in to turn off these ads.