对,下面是一个简单的 javascript 请求,用于显示 XML 文档中的内容。
我有很多不同版本的 XML,它们都来自同一个地方,但有些结构略有不同,因此KISCOURSE标记内的某些元素没有TITLE - 当这种情况发生时,它会抛出我的编码结构并使其看起来错误。
有没有办法,如果在KISCOURSE中找不到TITLE,则不要显示该KISCOURSE并移至下一个。
以防万一,有关 XML 数据外观的示例,请参阅:http ://universitycompare.com/wp-content/themes/blue-and-grey/XML/london_metropolitan_university.xml
<script type="text/javascript">
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","http://universitycompare.com/wp-content/themes/blue-and-grey/XML/london_metropolitan_university.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
var x=xmlDoc.getElementsByTagName("INSTITUTION");
for (i=0;i<x.length;i++)
var x=xmlDoc.getElementsByTagName("KISCOURSE");
for (i=0;i<x.length;i++)
{
document.write("<div class='CourseArea'><h3 id='CourseTitle'>");
// TITLE
document.write(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue);
document.write("</h3>");
document.write("<p class='ForP'>- ");
document.write(x[i].getElementsByTagName("MODE")[0].childNodes[0].nodeValue);
document.write("</p><div class='clear'></div>");
// UCAS CODE
document.write("<p id='ucascode'>");
document.write("<b>UCAS CODE</b> ");
document.write(x[i].getElementsByTagName("UCASCOURSEID")[0].childNodes[0].nodeValue);
document.write("</p>");
// READ MORE LIGHTBOX
document.write("<img src='<?php echo home_url(); ?>/wp-content/themes/blue-and-grey/images/info-icon.png' width='15' style='margin:0px 2px 0px -4px;' />");
document.write("<a class='various' href='#ReadMore'>Read More</a> ");
// VISIT COURSE LINK
document.write("<a class='courselink' href='");
document.write(x[i].getElementsByTagName("CRSEURL")[0].childNodes[0].nodeValue);
document.write("' target='_blank'>Visit course website</a>");
document.write("</div>");
}
</script>