Solved

Read XML and Populate a Drop Down Box

Posted on 2011-03-10
13
377 Views
Last Modified: 2012-06-21
I would like to know how to read XML elements and populate a drop down list with a particular element.
0
Comment
Question by:Lico_w
  • 7
  • 6
13 Comments
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
0
 

Author Comment

by:Lico_w
Comment Utility
I should have been more specific, I've attached my asp code below but it displays nothing. Any ideas? I have also attached a sample XML doc
<html>
<body>

<script type="text/javascript">
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.open("GET","depots.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML; 

document.write("<table border='1'>");
var x=xmlDoc.getElementsByTagName("depots");
for (i=0;i<x.length;i++)
  { 
  document.write("<tr><td>");
  document.write(x[i].getElementsByTagName("Depot")[0].childNodes[0].nodeValue);
  document.write("</td><td>");
  'document.write(x[i].getElementsByTagName("Depot")[0].childNodes[0].nodeValue);
  'document.write("</td></tr>");
  }
document.write("</table>");
</script>

</body>
</html>

Open in new window

depots.xml
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
malformed XML

your :
<?xml version="1.0"?>

<depots>

	<depot="Aberdeen">
		<address="some address"/>
	</depot>
</depots>

Open in new window



"corrected" :
<?xml version="1.0"?>

<depots>
	<depot attr1="Aberdeen">
		<address attr2="some address"/>
	</depot>
</depots>

Open in new window

0
 

Author Comment

by:Lico_w
Comment Utility
Can you post the update to my script to get it to work as i've updated the xml and  javascript but still doesn't work
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
you just need a table with a row having its cell with "Aberdeen" inside? That is ?
0
 

Author Comment

by:Lico_w
Comment Utility
No I want to read it from the XML. There are lots more elements and values within my full xml, didn't want to publish full depot info online.

If I can get this start point working I can build from that
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 82

Expert Comment

by:leakim971
Comment Utility
yes but for now what do you want to see from the xml file in the table cell with your sample?
0
 

Author Comment

by:Lico_w
Comment Utility
The depot name would be perfect
0
 

Author Comment

by:Lico_w
Comment Utility
I.e. Aberdeen
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
Comment Utility
With simple Javascript :


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Create table from XML</title>
<script type="text/javascript">
	window.onload = function() {
		if(window.XMLHttpRequest) xmlhttp=new XMLHttpRequest(); else  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		xmlhttp.open("GET","depots.xml",false);
		xmlhttp.send();
		xmlDoc = xmlhttp.responseXML; 
		var table = document.createElement("table");	
		table.setAttribute("border", "1");
		var x = xmlDoc.getElementsByTagName("depots");
		for(var i=0;i<x.length;i++) {
			var row = table.insertRow(table.rows.length);
			for(j=0;j<x[0].childNodes.length;j++) {
				if(x[0].childNodes[j].nodeType == 1) {
					var cell = row.insertCell(row.cells.length);
					var text = document.createTextNode( x[0].childNodes[j].getAttribute("attr1") );
					cell.appendChild( text );
				}
			}
	  }
	  document.getElementsByTagName("body")[0].appendChild(table);
	}
</script>
<body>
</body>
</html>

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
With jQuery :


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Create table from XML</title>
<script language="javascript" src="http://code.jquery.com/jquery-1.5.1.min.js"></script>
<script language="javascript">
	$(document).ready(function() {
		$.get("depots.xml", function(data) {
			$("body").append("<table border='1'></table>");
			$.each($("depot", data), function() {
				$("table").append("<tr><td>" + $(this).attr("attr1") + "</td></tr>");
			});
		});
	});
</script>
<body>
</body>
</html>

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
Your code << YES IT CAN >> :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Create table from XML</title>
<body>
<script type="text/javascript">
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.open("GET","depots.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML; 

document.write("<table border='1'>");
var x=xmlDoc.getElementsByTagName("depots");
for (i=0;i<x.length;i++)
  { 
			for(j=0;j<x[0].childNodes.length;j++) {
				if(x[0].childNodes[j].nodeType == 1) {
				  document.write("<tr><td>");
				  document.write(x[0].childNodes[j].getAttribute("attr1"));
				  document.write("</td>");
				}
			}
  }
document.write("</table>");
</script>
</body>
</html>

Open in new window

0
 

Author Closing Comment

by:Lico_w
Comment Utility
I can see why your ranked as genius!!!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now