/    Sign up×
Community /Pin to ProfileBookmark

Basic xml output via dom

Hi all, I’m just to loop through an xml file and append it to a p tag via inner HTML. I’m not getting any errors, but I output one peice of data from the xml instead of everything, I’m a little lost ? Any help/advice would be greatly appreciated.

[code]<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”
“http://www.w3.org/TR/html4/loose.dtd”>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″>
<script type=”text/javascript”>
function makeRequest(url) {

var http_request = false;

if (window.XMLHttpRequest) { // Mozilla, Safari …
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType(‘text/xml’);
// see note below about this line
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject(“Msxml2.XMLHTTP”);
} catch (e) {
try {
http_request = new ActiveXObject(“Microsoft.XMLHTTP”);
} catch (e) {}
}
}

if (!http_request) {
alert(‘giving up 🙁 cannot create XMLHTTP instance’);
return false;
}

http_request.onreadystatechange = function() { alertContents(http_request); };
http_request.open(‘GET’, url, true);
http_request.send(null);

}

function alertContents(http_request) {
try {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
var xmlDoc = http_request.responseXML;
var root_node = xmlDoc.getElementsByTagName(‘euLocales’);
var area2load = document.getElementById(‘print2Screen’);
for (var j = 0; j < root_node.length; j++) {
for (var i = 0; i < root_node[j].childNodes.length; i++) {
area2load.innerHTML = root_node[j].childNodes[i].nodeValue;
}
}
//area2load.innerHTML = root_node.firstChild.data;
} else {
alert(‘there was a problem with the request.’);
}
}
}
catch (e) {
alert(‘caught exception: ‘ + e.description);
}
}
window.onload = function() {makeRequest(‘dp.xml’)};
</script>
</head>

<body>
<p id=”print2Screen”></p>

</body>
</html>

[/code]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@gingerjauthorOct 14.2006 — Anyone? I'm a bit stumped!??
Copy linkTweet thisAlerts:
@konithomimoOct 14.2006 — The problem is with this line:

area2load.innerHTML = root_node[j].childNodes[i].nodeValue;





each time that you run through the loop it only outputs the last bit of info because every time you change the entire innerHTML of area2load. TO fix this simply make it:



area2load.innerHTML [color=red]+[/color]= root_node[j].childNodes[i].nodeValue;



the addition being the + highlighted in red. That way it adds to the innerHTML every time and does not simply replace it. To make it even better though, don't use innerHTML use firstChild.data:



area2load.firstChild.data += root_node[j].childNodes[i].nodeValue;



That way it will be DOM compliant and should work for most objects such as divs spans,table cells, etc.
×

Success!

Help @gingerj 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 5.8,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...