XML File Retrieval wont work on IE or Chrome - javascript

Why wont the following script work on Chrome or IE? It works on Safari and FF. I am just trying to find a basic way to retrieve data from an XML file and display it through HTML. Thanks!
<html>
<body>
<script>
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", "cd_catalog.xml", false);
xmlhttp.send();
xmlDoc = xmlhttp.responseXML;
document.write("<table border='1'>");
var x = xmlDoc.getElementsByTagName("CD");
for (i = 0; i < x.length; i++) {
document.write("<tr><td>");
document.write(x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue);
document.write("</td><td>");
document.write(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue);
document.write("</td></tr>");
}
document.write("</table>");
</script>
</body>
</html>

Related

Displaying Image from XML into HTML

I have tried to display the following image onto the html, however to no avail. When the html is loaded, it displays the picture icon, but it won't display the image itself. I have copied the code below
Thanks.
HTML Code:
<script>
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","test.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
document.write("<table border='1'>");
var x=xmlDoc.getElementsByTagName("TEST");
for (i=0;i<x.length;i++)
{
document.write(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue);
var img = document.createElement("img");
img.src = x[i].getElementsByTagName("imageurl")[0].childNodes[0].nodeValue;
document.write(img.src);
document.body.appendChild(img);
}
document.write("</table>");
</script>
XML:
<?xml version="1.0" encoding="UTF-8"?>
<TEST_GROUP>
<TEST>
<TITLE>Hello World</TITLE>
<imageurl>"CFL_175x175.png"</imageurl>
</TEST>
</TEST_GROUP>
Try this:
<imageurl>CFL_175x175.png</imageurl>

How to run a script locally

I am trying to display XML in an HTML page
<html>
<body>
<script>
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","cd_catalog.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
document.write("<table border='1'>");
var x=xmlDoc.getElementsByTagName("CD");
for (i=0;i<x.length;i++)
{
document.write("<tr><td>Author</td><td>");
document.write(x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue);
document.write("</td></tr><tr><td>Song</td><td>");
document.write(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue);
document.write("</td></tr>");
}
document.write("</table>");
</script>
</body>
</html>
It works like a charm in W3School Try it yourself section, but I want to run it from my local computer, how can i do this?

getElementsByTagName how to get xml data

I'm using this scipt to make a browser compatible xml reader.
However I never used one of these badboys, and I have no idea how can I get the data from the object tag below.
I need to print it int this part: document.write(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue);
But here is the full code (with the object xml tag.
<script>
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","myxml.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
var x=xmlDoc.getElementsByTagName("property");
for (i=0;i<x.length;i++)
{
document.write("<table border='1'>");
document.write("<tr><td>Utolsomod</td><td>");
document.write(x[i].getElementsByTagName("EPOLeafNode.LastUpdate")[0].childNodes[0].nodeValue);
document.write("</td></tr><tr><td>Song</td><td>");
document.write(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue);
document.write("</td></tr>");
document.write("</table>");
document.write("<br/>");
}
</script>
<object index="20">
<property name="name1">*somedate*</property>
<property name="name2">*somename*</property>
<property name="name3">*someip*</property>
</object>
try
document.write(xmlDoc.getElementsByTagName("EPOLeafNode.LastUpdate")[0].childNodes[0].nodeValue);
I was able to figure it out as: document.write(x[i].getElementsByTagName("property")[0].childNodes[0].nodeValue);
Thank you guys for helping. :)
try this
XmlDocument axmlDoc = new XmlDocument();
axmlDoc.Load(myxml.xml);
XmlNodeList prop = axmlDoc.GetElementsByTagName("property");
prop[i].InnerText

Windows 8 javascript html5 app not parsing xml

My javascript XML parsing code works fine when I load it in firefox using local HTML & XML files, however when i build my app in visual studio 2012 (HTML5 javascript app) using the same code it fails to parse the XML file.
<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", "pages/home/example.xml", false);
xmlhttp.send();
xmlDoc = xmlhttp.responseXML;
//Content
txt = xmlDoc.getElementsByTagName("Symbol")[0].childNodes[0].nodeValue;
document.write(txt + " ");
You can get responseXML only after load.
var xmlDoc
xmlhttp.open("GET", "pages/home/example.xml", false);
xmlhttp.onload = function(){
xmlDoc = xmlhttp.responseXML
}
xmlhttp.send();

I want to get the values in array and pass it to some location

<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", "products.xml", false);
xmlhttp.send();`enter code here`
xmlDoc = xmlhttp.responseXML;
document.write("<table border='1'>");
var x = xmlDoc.getElementsByTagName("productItem");
for (i = 0; i < x.length; i++) {
document.write("<tr><td>");
document.write(x[i].getElementsByTagName("ProName")[0].childNodes[0].nodeValue);
document.write("</td><td>");
document.write(x[i].getElementsByTagName("ProRate")[0].childNodes[0].nodeValue);
document.write("</td></tr>");
}
document.write("</table>");
</script>
This is my JavaScript coding.. I am fetching the values from XML file..
so i want to get these values from an array and to pass in some other place
Take a look at jQuery and it's plugin jQuery Templates

Categories

Resources