We help IT Professionals succeed at work.

display images from xml file

517 Views
Last Modified: 2013-11-05
I have an XML file which contains (links to) my images, and captions. I would like to display them in a table on my page. I'm using the javascript below. Of course, the images are not showing up, only the paths are written out.

Can someone help me display the actual images?
<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","image-flow.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML; 

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

Open in new window

Comment
Watch Question

KNVB HKSITO
CERTIFIED EXPERT

Commented:
can you attach the XML file in here?
leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
@cstsang check this : https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/XML/Q_26295053.html
Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
Another method :


<!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" />
<script type="text/javascript">
	function init() {
		if(window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); } else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
		xmlhttp.open("GET","image-flow.xml",false);
		xmlhttp.send();
		xmlDoc = xmlhttp.responseXML;
		if(xmlDoc != null) { 
			var table = document.createElement("table");
			table.border = "1";
			var x = xmlDoc.getElementsByTagName("img");
			for (i=0;i<x.length;i++) {
				var tr = document.createElement("tr");
				var td1 = document.createElement("td");
				var img = document.createElement("img");
				img.setAttribute("src", x[i].getElementsByTagName("src")[0].childNodes[0].nodeValue);
				td1.appendChild(img);
				var td2 = document.createElement("td");
				text = document.createTextNode(x[i].getElementsByTagName("caption")[0].childNodes[0].nodeValue);
				td2.appendChild(text);
				tr.appendChild(td1);
				tr.appendChild(td2);
				table.appendChild(tr);
			}
			document.getElementById("mydiv").appendChild(table);
		}
		else {
			alert("failed");
		}
	}
</script>
</head>
<body onload="init();">
<div id="mydiv"></div>
</body>
</html>

Open in new window

leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
work fine with :


<?xml version="1.0" encoding="utf-8"?>
<bank> 
 <img> 
  <src>ballard.jpg</src>
  <caption>capt</caption>
 </img> 
</bank>

Open in new window

Kyle HamiltonData Scientist
Most Valuable Expert 2014

Author

Commented:
Thank you. This worked perfectly. (just so you know, the one after it didn't work on the PC).

My next step is how to resize the images with javascript, so they fit  nicely into my predetermined table boxes. I'll open up a new question: Resizing images in javascript.

Thanks again

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.