/    Sign up×
Community /Pin to ProfileBookmark

Displaying XML Data in HTML page

Hi Javascript Gurus,

I am a beginner in java , and i am struggling to display an XML File (saved locally) using javascript that i have in an HTML Page, i am using Chrome although i did try firefox and IE which do not WORK as well.

I think the code is correct as this example is on the internet, i have also read that browsers do not allow ACCESS to the file system by default
Is there a way i can get this WORKING? I am using Dreamwever as my platform. Here is the HTML and Java code below.

Below is the Javascript which is embedded in an html page, Its purpose is to go and read a local XML File saved in
the local directory called cd_catalog.xml and displays it on the html page at the click of a button.

<html>
<head>
<script>
function loadXMLDoc(url)
{
var xmlhttp;
var txt,xx,x,i;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject(“Microsoft.XMLHTTP”);
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
txt=”<table border=’1′><tr><th>Title</th><th>Artist</th></tr>”;
x=xmlhttp.responseXML.documentElement.getElementsByTagName(“CD”);
for (i=0;i<x.length;i++)
{
txt=txt + “<tr>”;
xx=x[i].getElementsByTagName(“TITLE”);
{
try
{
txt=txt + “<td>” + xx[0].firstChild.nodeValue + “</td>”;
}
catch (er)
{
txt=txt + “<td>&nbsp;</td>”;
}
}
xx=x[i].getElementsByTagName(“ARTIST”);
{
try
{
txt=txt + “<td>” + xx[0].firstChild.nodeValue + “</td>”;
}
catch (er)
{
txt=txt + “<td>&nbsp;</td>”;
}
}
txt=txt + “</tr>”;
}
txt=txt + “</table>”;
document.getElementById(‘txtCDInfo’).innerHTML=txt;
}
}
xmlhttp.open(“GET”,url,true);
xmlhttp.send();
}
</head>
<body>

<div id=”txtCDInfo”>
<button onclick=”loadXMLDoc(‘cd_catalog.xml’)”>Get CD info</button>
</div>

</body>
</html>

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @Msyhles spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...