Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

javascript help of ignoring nodes if they do not exist in XML

Featured Replies

Right, Below is a simple javascript request to display the content that is in the XML document.

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.

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.

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

 

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

  • Author

Why not use XSLT?

 

haven't really dove into XSLT to see what it's like, and the information that I receive each year will be XML, figured it would be easier to sort or is easy to convert over to XSLT?

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.