0

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
  • 313
  • 6
  • 18
  • Possible duplicate of [How to check if one string contains another substring in JavaScript?](http://stackoverflow.com/questions/1789945/how-to-check-if-one-string-contains-another-substring-in-javascript) – Justinas Sep 07 '16 at 21:02
  • In addition to simply looping over the elements, using the technique cited by @Justinas to find if the value contains the string you're looking for, you could also use [XPath in JavaScript](https://developer.mozilla.org/en-US/docs/Introduction_to_using_XPath_in_JavaScript). Depending on how many nodes you have, browser support, etc., just looping through them and checking the value could be faster. – Heretic Monkey Sep 07 '16 at 21:17
  • @Justinas Mike McCaughan i'm trying to use the below. Can you tell me if thats the correct approach? ` for (i = 0; i < x.length; i++) { if('txt'.indexOf('lang=en')) { txt += x[i].childNodes[0].nodeValue + "
    "; } }`
    – user2628187 Sep 07 '16 at 22:41
  • Not quite, since that code tests if the literal string "txt" contains "lang=en", which of course it doesn't. You want to test if `x[i].childNodes[0].nodeValue` contains "lang=en". Also, you'll want to check that `indexOf()` does not return `-1`. – Heretic Monkey Sep 07 '16 at 22:48

0 Answers0