I am looking to reverse the order of this list using JavaScript. I have tried different methods that I know, but the below code shows it in a straight manner. I am not familiar with XML nodes and how to completely reverse it.
<messages>
<messageset>
<name>torje</name>
<time>1533904431</time>
<message>Vvvhjhf</message>
</messageset>
<messageset>
<name>moneyman</name>
<time>1533904437</time>
<message>njkjlmkmkl</message>
</messageset>
<messageset>
<name>anjali</name>
<time>1533904445</time>
<message>A hi fyi bk MLS egg FG ch bhi CDG jk IC</message>
</messageset>
</messages>
In the current code, it displays the table in the following order:
torje - Vvvhjhf, moneyman - njkjlmkmkl, anjali - A hi fyi bk MLS egg FG ch bhi CDG jk IC
. Instead, I want to reverse it so that Anjali's message is displayed first, then Moneyman, and finally Torje (apologies for any errors in the messages).
function fetch() {
setTimeout( function() {
loadDoc()
fetch();
}, 100);
}
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
}
};
xhttp.open("GET", "../clubs/club.xml", true);
xhttp.send();
}
function myFunction(xml) {
var i;
var xmlDoc = xml.responseXML;
var table="<tr><th>NAME</th><th>message</th></tr>";
var x = xmlDoc.getElementsByTagName("messageset");
for (i = 0; i < x.length; i++) {
table += "<tr><td>" +
x[i].getElementsByTagName("name")[0].childNodes[0].nodeValue +
"</td><td>" +
x[i].getElementsByTagName("message")[0].childNodes[0].nodeValue +
"</td></tr>";
}
document.getElementById("demo").innerHTML = table;
}