简体   繁体   English

XML的HTTPRequest-仅在有特定子子级时显示

[英]HTTPRequest for XML - only appear if there is a certain sub-child

Right, Below is a simple javascript request to display the content that is in the XML Document. 正确,下面是一个简单的javascript请求,用于显示XML文档中的内容。

I have lot's of different versions of XML's that are all from the same place but some are structured slightly different, so some elements inside the KISCOURSE tag do not have a TITLE - when this happens it throws out my coding structure and makes it appear wrong. 我有许多不同版本的XML,这些版本都来自同一地方,但有些结构略有不同,因此KISCOURSE标记内的某些元素没有TITLE-发生这种情况时,它会抛出我的编码结构,并使其显示错误。

Is there a way, if TITLE is not found inside a KISCOURSE , then do not display that KISCOURSE and just move onto the next one. 有没有办法,如果在KISCOURSE内未找到TITLE ,则不显示该KISCOURSE,而移至下一个。

Just incase, For an example on how the XML Data looks, see: http://universitycompare.com/wp-content/themes/blue-and-grey/XML/london_metropolitan_university.xml 以防万一,有关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'>-&nbsp;&nbsp;");
  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>&nbsp;&nbsp;&nbsp;");
  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>&nbsp;&nbsp;&nbsp;");

  // 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>

You can continue (skip to the next loop iteration) if the title is missing: 如果标题丢失,则可以继续(跳到下一个循环迭代):

var x=xmlDoc.getElementsByTagName("KISCOURSE");
for (i=0;i<x.length;i++)
{ 
  if(x.getElementsByTagName("TITLE").length == 0) continue; //this skips to the next.

  document.write("<div class='CourseArea'><h3 id='CourseTitle'>");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM