Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

Use an html form to select data from an xml file

8:10 pm on Jan 20, 2009 (gmt 0)

New User

10+ Year Member

joined:Nov 22, 2008
posts: 23
votes: 0

beginner question (please tell me if I'm in the wrong forum!)
I'm wanting to use an html form to query an xml file in the same way that I would use an html form to query a mySQL database

I want to create an html form where a user enters a some text (an artists name for example) & then that string is used as the search string to find matches in my paintings.xml file and then return the results to a web page.

Can anyone point me in the direction of a tutorial for this.

Thank you for your help.

9:10 pm on Jan 20, 2009 (gmt 0)

Senior Member from CA 

WebmasterWorld Senior Member httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Aug 29, 2003
votes: 0

you'll probably want to parse the XML file in PHP, then find your node using XPATH.

There are plenty of tutorials on each, if you hit the search engines for "PHP XPATH".

And I do recommend the SimpleXML [ca3.php.net] class, which has an XPATH [ca3.php.net] method.

6:09 pm on Jan 24, 2009 (gmt 0)

New User

10+ Year Member

joined:Nov 22, 2008
votes: 0

Thanks httpwebwitch.
I have this .js file that I think will pretty much do for what I need. As it stands, if I type a name <title> into my form, the .js file looks through my XML file for a matching name & returns that name to my page (which isn't achieving much really!)
How can I adapt the code so that it returns the rest of the child elements along with it ie. <author> <book_title>
Thank you to any kind expert who can help me out!

window.onload = loadIndex;
function loadIndex() { // load indexfile
if (document.implementation && document.implementation.createDocument) {
xmlDoc = document.implementation.createDocument("", "", null);
else if (window.ActiveXObject) {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false";
function searchIndex() {
if (!xmlDoc) {
var searchterm = document.getElementById("searchme").value;
var allitems = xmlDoc.getElementsByTagName("title");
results = new Array;
if (searchterm.length < 2) {
alert("Enter at least two characters");
} else {
for (var i=0;i<allitems.length;i++) {
var name = allitems[i].lastChild.nodeValue;
var exp = new RegExp(searchterm,"i");
if ( name.match(exp) != null) {
showResults(results, searchterm);
function showResults(results, searchterm) {
if (results.length > 0) {
var resultshere = document.getElementById("resultshere");
var header = document.createElement("h5");
var list = document.createElement("ul");
var searchedfor = document.createTextNode("You've searched for "+searchterm);
for (var i=0;i<results.length;i++) {
var listitem = document.createElement("li");
var item = document.createTextNode(results[i].lastChild.nodeValue);
} else {
var resultshere = document.getElementById("resultshere");
var para = document.createElement("p");
var notfound = document.createTextNode("Sorry, I couldn't find anything like "+searchterm +"!");

[edited by: httpwebwitch at 3:06 am (utc) on Jan. 26, 2009]
[edit reason] normalized font size [/edit]

3:34 am on Jan 26, 2009 (gmt 0)

Senior Member from MY 

WebmasterWorld Senior Member vincevincevince is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Apr 1, 2003
votes: 0

var item = document.createTextNode(results[i].lastChild.nodeValue);

That still points you to the 'title' element which you matched; to find others jump to the parent then find the child:

var itemobject = results[i].parentNode;
var item_title = itemobject.getElementsByTagName('title')[0];
var item_summary = itemobject.getElementsByTagName('summary')[0];

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members