I have a html code that is supposed to create a xmlDoc object, read an XML code into it from a file, get a value of a node and print it on screen. However, the code below is executed but does not read the value. What I get is just empty string I guess. I tried .firstChild and the result was the same:
<text> is :)
Between "<text> and "is :)" I'd expect "My child no 1.". Thanks for any help.
The html code:
<!DOCTYPE html>
<html>
<head>
<title>Test html</title>
<script src="loadxmldoc.js"></script>
</head>
<body>
<script>
var xmlDoc = loadXMLDoc("test_xml.xml");
var x = xmlDoc.getElementsByTagName("child")[0].childNodes[0];
var txt = x.nodeValue;
document.write("<txt> is " + txt + " :)");
</script>
</body>
</html>
XML test_xml.xml code:
<?xml version="1.0" encoding="UTF-8" ?>
<root>
<children>
<child id="1">
<txt>My child no 1.</txt>
</child>
<child id="2">
<txt>My child no 2.</txt>
</child>
<child id="3">
<txt>My child no 3.</txt>
</child>
<child id="4">
<txt>My child no 4.</txt>
</child>
</children>
</root>
and loadXMLDoc function in loadxmldoc.js file:
function loadXMLDoc(filename) {
if (window.XMLHttpRequest) {
xhttp=new XMLHttpRequest();
} else { // code for IE5 and IE6
xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET",filename,false);
xhttp.send();
return xhttp.responseXML;
}
No comments:
Post a Comment