Jump to content

select one record from xml in javascript


henny

Recommended Posts

hello,I've a XML with for example agenda-content ( see example below - just a dummy XML )I can display this agenda in an HTML-table with only <date> & <less> information incl. a hyperlink using XSL when the user clicks the hyperlink,my current implementation- starts a javascript-function ( with <date> & <more> as parameters )- opens a new browser window- and displays the content ( using both parameters )for a couple of parameters, passing them into the function that's good enoughbut what I realy would like to do, is passing just the <key> as only parameter to the javascriptand- opens the XML file in the window ( load the XML into DOM for example )- selects only that record with the correct <key> to display <date> & <more> info suggestions welcomethx a lot in advance - Henny

<?xml version="1.0" encoding="ISO-8859-1"?><agenda>	<activity>		<key>20060531</key>		<date>wednesday 31 may 2006</date>		<less>what you normally do on a wednesday in may</less>		<more>more details that will be displayed in popup-window, the question is how can I do that in a javascript-function</more>	</activity>	<activity>		<key>20060612</key>		<date>monday 12 juni 2006</date>		<less>what you normally do on a monday in juni</less>		<more>more details that will be displayed in popup-window, the question is how can I do that in a javascript-function</more>	</activity>	<activity>		<key>20060617</key>		<date>saturday 17 juni 2006</date>		<less>what you normally do on a saturday in juni</less>		<more>more details that will be displayed in popup-window, the question is how can I do that in a javascript-function</more>	</activity>	<activity>		<key>20060909</key>		<date>saturday 9 september 2006</date>		<less>what you normally do on a saturday in september</less>		<more>more details that will be displayed in popup-window, the question is how can I do that in a javascript-function</more>	</activity></agenda>

Link to comment
Share on other sites

If you have any say over the structure of your XML, I would suggest changing it so that it was more like this:

<activity key="20060531"> <date>wednesday 31 may 2006</date> <less>what you normally do on a wednesday in may</less> <more>more details that will be displayed in popup-window, the question is how can I do that in a javascript-function</more></activity>
Then, with javascript, you can get all the activity nodes and loop through until you find the one with the appropriate key attribute:
var key = "20060531";var activities = MyXMLDoc.getElementsByTagName("activity");for(var i = 0; i < activities.length; i++){	if(activities[i].getAttribute("key") && activities[i].getAttribute("key") == key)	{		// activities[i] is the node you are looking for.	}}

If you cannot change the structure of your XML, then maybe you could do something like this (untested):

var key = "20060531";var activities = MyXMLDoc.getElementsByTagName("activity");for(var i = 0; i < activities.length; i++){	var keyNode = activities[i].getElementsByTagName("key")[0];	if(keyNode && keyNode.nodeValue == key)	{		// activities[i] is the node you are looking for.	}}

For loading the XML into the DOM, I would suggest AJAX. As long as your XML is being loaded from a file and as long as it is properly formatted (i.e. no syntax errors), the responseXML value of the XMLHttpRequest will return an XML object that you can traverse using the DOM.For more info, check out both the XML DOM and AJAX tutorials:http://www.w3schools.com/dom/default.asphttp://www.w3schools.com/ajax/default.asp

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...