homepage Welcome to WebmasterWorld Guest from 23.20.77.156
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / XML Development
Forum Library, Charter, Moderators: httpwebwitch

XML Development Forum

    
Use an html form to select data from an xml file
egibberate




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

Hi,
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.

 

httpwebwitch




msg:3830677
 9:10 pm on Jan 20, 2009 (gmt 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.

egibberate




msg:3833861
 6:09 pm on Jan 24, 2009 (gmt 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);
xmlDoc.load("index.xml");
}
else if (window.ActiveXObject) {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false";
xmlDoc.load("index.xml");
}
}
function searchIndex() {
if (!xmlDoc) {
loadIndex();
}
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) {
results.push(allitems[i]);
}
}
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);
resultshere.appendChild(header);
header.appendChild(searchedfor);
resultshere.appendChild(list);
for (var i=0;i<results.length;i++) {
var listitem = document.createElement("li");
var item = document.createTextNode(results[i].lastChild.nodeValue);
list.appendChild(listitem);
listitem.appendChild(item);
}
} else {
var resultshere = document.getElementById("resultshere");
var para = document.createElement("p");
var notfound = document.createTextNode("Sorry, I couldn't find anything like "+searchterm +"!");
resultshere.appendChild(para);
para.appendChild(notfound);
}
}

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

vincevincevince




msg:3834468
 3:34 am on Jan 26, 2009 (gmt 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];
etc.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / XML Development
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved