I need to parse XML parse with HTML and javascript.
My XML:
<tv_channel id = "1">
<movie>
<name>Movie 1</name>
<time>6:00</time>
</movie>
<movie>
<name>Movie 2</name>
<time>6:30</time>
</movie>
</tv_channel>
<tv_channel id = "2">
<movie>
<name>Movie 3</name>
<time>11:15</time>
</movie>
<movie>
<name>Movie 4</name>
<time>13:45</time>
</movie>
</tv_channel>
and my Javascript is
function vyberZaner(zaner, nazov, from, to) {
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET", "XML.xml", false);
xmlhttp.send();
xmlDoc = xmlhttp.responseXML;
var txt = document.getElementById('div_table')
var content = '';
content += '<table border=\'1\'>';
var x = xmlDoc.getElementsByTagName("tv_channel");
content += '<h1 > ' + nazov + ' </h1>';
content += ("<td> NAME </td>");
content += ("<td> GENRE </td>");
for (i = 0; i < x.length; i++) {
content += '<tr><td>';
content += x[i].getElementsByTagName("name")[0].childNodes[0].nodeValue;
content += '</td><td>';
content += x[i].getElementsByTagName("time")[0].childNodes[0].nodeValue;
content += '</td></tr>';
}
content += '</table>';
txt.innerHTML = content;
}
And the output is
Movie 1 6:00
Movie 2 6:00
Movie 3 6:00
Movie 4 6:00
and I need just two
Movie 1 6:00
Movie 2 6:00
or
Movie 3 6:00
Movie 4 6:00
It depends on id but I don’t know how to do that.
I need just to get output like only “Movie 1 and Movie 2” or “Movie 3 and Movie 4”. The user will choose with button to select xml and display only the channel’s info with id 1 or id 2 like and their name and time.
Use the getAttribute method to determine whether to pull the XML for channel id 1 or channel id 2, and then output that content. Inside the for loop, use:
if(x[i].getAttribute(‘id’) == 1or2) {do whatever}
See: http://www.w3schools.com/dom/met_element_getattribute.asp