I have an XML file:
<?xml version="1.0" encoding="UTF-8"?>
<products>
<product>
<id>246</id>
<code>Ash07-001</code>
<image>C:\BowlPhotos\Thumbs\Ash07-001tmb.jpg</image>
</product>
<product>
<id>247</id>
<code>Ash07-004</code>
<image>C:\BowlPhotos\Thumbs\NoBowltmb.jpg</image>
</product>
<product>
<id>248</id>
<code>Ash07-005</code>
<image>C:\BowlPhotos\Thumbs\Ash07-005tmb.jpg</image>
</product>
</products>
And read it with this code:
document.write("<table border='1'>");
var x=xmlDoc.getElementsByTagName("product");
for (i=0;i<x.length;i++)
{
document.write("<tr><td>");
document.write(x[i].getElementsByTagName("id")[0].childNodes[0].nodeValue);
document.write("</td><td>");
document.write(x[i].getElementsByTagName("code")[0].childNodes[0].nodeValue);
document.write("</td><td>");
document.write(x[i].getElementsByTagName("image")[0].childNodes[0].nodeValue);
document.write("</td></tr>");
}
document.write("</table>");
And that works to get all of the file.
What if I only want the product with the id of 247? How do I pull that entire product out and print just that one?
document.write("<table border='1'>");
var x=xmlDoc.getElementsByTagName("product");
for (i=0;i<x.length;i++)
{
if(x[i].getElementsByTagName("id")[0].childNodes[0].nodeValue != 247) continue;
document.write("<tr><td>");
document.write(x[i].getElementsByTagName("id")[0].childNodes[0].nodeValue);
document.write("</td><td>");
document.write(x[i].getElementsByTagName("code")[0].childNodes[0].nodeValue);
document.write("</td><td>");
document.write(x[i].getElementsByTagName("image")[0].childNodes[0].nodeValue);
document.write("</td></tr>");
}
document.write("</table>");
You can write code like this . It will not execute code for displaying the content if the id is not 247
var x=xmlDoc.getElementsByTagName("product");
for (i=0;i<x.length;i++)
{
var id= x[i].getElementsByTagName("id")[0].childNodes[0].nodeValue;
if id="247"
{
// print what ever u want
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.