Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Xml value to Select Box options 1

Status
Not open for further replies.

Farayic

Programmer
Sep 25, 2008
2
I am trying to fill a select box with xml values. There is nothing filling in. Please help:

Here is my code and xml file:


HTML FILE:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "
<!-- XML in easy steps - Page 164. -->

<html xmlns="
<head>

<title>Get All Data</title>
<!-- XML in easy steps - p.164-165 -->

<meta http-equiv = "Content-Type" content = "text/html;charset=UTF-8" />

<script type="text/javascript">
/* <![CDATA[ */


var xmlDoc;

function loadXmlDoc()
{
try
{
if (window.ActiveXObject)
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load("music.xml");
LoadOptions;
}
else if (document.implementation && document.implementation.createDocument)
{
xmlDoc = document.implementation.createDocument("","",null);
xmlDoc.load("music.xml");
xmlDoc.onload=LoadOptions;
}
}
catch(err)
{

document.getElementById("box").innerHTML = "XML data is unavailable";
}
}

var genrename;

function LoadOptions()
{

genrename = xmlDoc.getElementsByTagName("Name");
for (var i=0; i<genrename.length; i++)
{
document.forms['genreform'].genre.options = new Option(genrename.text ,genrename.text);
}
}

window.onload=loadXmlDoc;
//window.onload=loadOptions;

/* ]]> */
</script>

</head>

<body>
<form name="genreform">
<select name="genre" >
<option value="Test"> Testing</Option>
</select>
</form>
<div id = "box" style= "background:yellow;width:320px;padding:5px"> </div>

</body>

</html>



XML FILE:

<?xml version="1.0" encoding="ISO8859-1" ?>
<NewDataSet>
<CD>
<Name>Music</Name>
</CD>
<CD>
<Name>Politics</Name>
</CD>
<CD>
<Name>Shopping</Name>
</CD>
</NewDataSet>
 
If you're trying to run this locally (i.e. file:// instead of http:// protocol), then this may well be your problem. I think you need to run through a web server to get AJAX working.

try uploading your code and trying again. Failing that, post in forum1600 which is more suited to AJAX questions than here.

Hope this helps,
Dan


Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
[tt][ignore]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "[/ignore]

<!-- XML in easy steps - Page 164. -->

<html [ignore]xmlns="[/ignore]>

<head>

<title>Get All Data</title>
<!-- XML in easy steps - p.164-165 -->

<meta http-equiv = "Content-Type" content = "text/html;charset=UTF-8" />

<script type="text/javascript">
/* <![CDATA[ */


var xmlDoc;

function loadXmlDoc()
{
try
{
if (window.ActiveXObject)
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load("music.xml");
LoadOptions[red]()[/red]; [red]//watch parentheses[/red]
}
else if (document.implementation && document.implementation.createDocument)
{
xmlDoc = document.implementation.createDocument("","",null);
[red]//watch the order
xmlDoc.onload=LoadOptions;
xmlDoc.load("music.xml");[/red]
}
}
catch(err)
{

document.getElementById("box").innerHTML = "XML data is unavailable";
}
}

var genrename;

function LoadOptions()
{

genrename = xmlDoc.getElementsByTagName("Name");
for (var i=0; i<genrename.length; i++)
{
document.forms['genreform'].genre.options = new Option(genrename.[red]firstChild.nodeValue[/red],genrename.[red]firstChild.nodeValue[/red]);
}
}

window.onload=loadXmlDoc;
//window.onload=loadOptions;

/* ]]> */
</script>
[/tt]
 
Thanks Tsuji your valued suggestions did the trick.

Best regards,

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top