I'm trying to add an IF statement to this javascript that will select/display only those xml nodes either with a certain attribute or attribute value.
Here's my xml code:
Expand|Select|Wrap|Line Numbers
- <stations>
- <station timezone="PT" PT="">
- <city>Seattle</city>
- <freq>93.1 FM</freq>
- </station>
- <station timezone="MT" MT="">
- <city>Phoenix</city>
- <freq>95.7 FM</freq>
- </station>
- <station timezone="CT" CT="">
- <city>Nashville</city>
- <freq>99.9 FM</freq>
- </station>
- <station timezone="ET" ET="">
- <city>Atlanta</city>
- <freq>107.9 FM</freq>
- </station>
- </stations>
Here's the one that works in FF (but not IE6):
Expand|Select|Wrap|Line Numbers
- <script type="text/javascript">
- xmlDoc=loadXMLDoc("stations.xml");
- x=xmlDoc.getElementsByTagName("city");
- for (i=0;i<x.length;i++)
- {
- if (x[i].parentNode.hasAttribute("MT"))
- {
- document.write(x[i].childNodes[0].nodeValue)
- document.write("<br />")
- }
- }
- </script>
Expand|Select|Wrap|Line Numbers
- <script type="text/javascript">
- xmlDoc=loadXMLDoc("stations.xml");
- xmlDoc.setProperty("SelectionLanguage", "XPath");
- x=xmlDoc.selectNodes("//station[@tmzn='MT']/city");
- for (i=0;i<x.length;i++)
- {
- document.write(x[i].childNodes[0].nodeValue)
- document.write("<br />")
- }
- </script>
Expand|Select|Wrap|Line Numbers
- function loadXMLDoc(dname)
- {
- var xmlDoc;
- // code for IE
- if (window.ActiveXObject)
- {
- xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
- }
- // code for Mozilla, Firefox, Opera, etc.
- else if (document.implementation && document.implementation.createDocument)
- {
- xmlDoc=document.implementation.createDocument("","",null);
- }
- else
- {
- alert('Your browser cannot handle this script');
- }
- xmlDoc.async=false;
- xmlDoc.load(dname);
- return(xmlDoc);
- }
Thanks!