I'm using a javascript to read a XML and print a particular node in HTML which is working fine with the below code.
I'm looking to filter the nodes based on the language of the url.
For example I want to show output like this
http://www.w3schools.com/xml/xml_applications.asp?**lang=en**
http://www.w3schools.com/xml/apps.asp?**lang=en**
This is the XML i'm trying to read. Currently with the below JS its printing all the nodes in the HTML file, I would like to filter and show the URLs only that has lang=en in it.
<?xml version="1.0" ?>
<bookstore xmls="http://www.google.com/sitemap.0.9">
<book category="">
<loc>http://www.w3schools.com/xml/xml_applications.asp?**lang=en**</loc>
</book>
<book category="">
<loc>http://www.w3schools.com/xml/xml_applications.asp?lang=fr</loc>
</book>
<book category="">
<loc>http://www.w3schools.com/xml/xml_applications.asp?lang=de</loc>
</book>
<book category="">
<loc>http://www.w3schools.com/xml/apps.asp?**lang=en**</loc>
</book>
</bookstore>
The JS that prints all nodes and output in HTML
<script>
var xhttp;
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
}
};
xhttp.open("GET", "writers.xml", true);
xhttp.send();
function myFunction(xml) {
var x, i, txt, xmlDoc;
xmlDoc = xml.responseXML;
txt = "";
x = xmlDoc.getElementsByTagName("loc");
for (i = 0; i < x.length; i++) {
txt += x[i].childNodes[0].nodeValue + "<br>";
}
document.getElementById("demo").innerHTML = txt;
}
</script>
Can you please provide any pointers to filter the URLs with a match condition?
Is it correct approach to use
for (i = 0; i < x.length; i++) {
if('txt'.indexOf('lang=en')) {
txt += x[i].childNodes[0].nodeValue + "<br>";
}
}
"; } }` – user2628187 Sep 07 '16 at 22:41