Hi..
I have an XML file like this
<?xml version="1.0" encoding="UTF-8"?>
<player>
<user>
<username>destiya</username>
<password>destiya</password>
</user>
<user>
<username>dian</username>
<password>dian</password>
</user>
</player>
then I want to parse it with javascript
and here is my javascript code :
function loadXML(path) {
var xhttp;
xhttp = new XMLHttpRequest();
xhttp.open("GET", path, false);
xhttp.send();
return xhttp.responseXML;
}
var xmlDoc = loadXML('db.xml');
user = xmlDoc.getElementsByTagName('player');
for(i = 0; i < user.length; i++) {
alert(user[i].childNodes[0].childNodes[0].wholeText);
}
When I run the javascript, the alert appear with no value. I expect that it must be username value come with the alert.
When I tried to log console.log(xmlDoc.getElementsByTagName('player')[0].childNodes[0])
I saw that it just result <TextNode textContent="\n">
.
It means that every end of the line read as \n character by parser. So I had to remove all line ending in my xml file.
It's annoying to have xml file without tab or space.
So, how can I still use my original xml file and make parser read element's line-ending just as line-ending?
I hope someone can help me.
Thank you.