Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Aug 2009
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Checking XML node for null or undefined

    Hi, im having a little difficulty checking if an XML node has a value, here the code:

    var Divs=new Array("artist","bio","img","date","tickets","venue","street","city","country","headliner");

    xmlDoc=xmlhttp.responseXML;

    for ( nodes in Divs )
    {

    if(!xmlDoc.getElementsByTagName(Divs[nodes])[0].childNodes[0].nodeValue)
    {
    }
    else
    {
    document.getElementById(Divs[nodes]).innerHTML=
    xmlDoc.getElementsByTagName(Divs[nodes])[0].childNodes[0].nodeValue+"<br>";
    }

    }

    This always throws up the error: document.getElementById(Divs[nodes]) is null

    ive tried putting the xmlDoc in a variable then checking if its null but no luck, also tried the same method against "undefined" but no luck either.

    Would be greatful if anyone has any suggestions.

    Thanks, Tom.

  • #2
    Senior Coder ckeyrouz's Avatar
    Join Date
    Jun 2009
    Location
    Montreal, Canada
    Posts
    1,044
    Thanks
    5
    Thanked 179 Times in 179 Posts
    Can you please you xmlDoc coming from the following :
    Code:
    xmlDoc=xmlhttp.responseXML;
    in order to track the problem?

  • #3
    New to the CF scene
    Join Date
    Aug 2009
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Its a php file that generates the XML file, code if as follows:


    echo '<?xml version="1.0" encoding="ISO-8859-1"?>';
    echo "<gig>";
    echo "<artist>" . $result['name'] . "</artist>";
    echo "<bio>" . $result['bio']['summary'] . "</bio>";
    echo "<img>" . $result['image']['medium'] . "</img>";

    echo "<date>" . $result['startdate'] . "</date>";
    echo "<tickets>" . $result['tickets'] . "</tickets>";

    echo "<venue>" . $result['venue']['name'] . "</venue>";
    echo "<street>" . $result['venue']['location']['street'] . "</street>";
    echo "<city>" . $result['venue']['location']['city'] . "</city>";
    echo "<country>" . $result['venue']['location']['country'] . "</country>";

    echo "<headliner>" . $result['headliner'] . "</headliner>";
    echo "</gig>";


    there is likely to be a node that doesn't contain a value, that's why I want to check for nodes will no value and then ignore them


    thanks

  • #4
    Senior Coder ckeyrouz's Avatar
    Join Date
    Jun 2009
    Location
    Montreal, Canada
    Posts
    1,044
    Thanks
    5
    Thanked 179 Times in 179 Posts
    I got your point what I am meaning is, is your data arriving to the browser?

    What I need from you is the ouput of the xml on the browser not on the server side (inside the PHP file).

    you can do it in many ways:
    Code:
    window.open().document.write(xmlhttp.responseXML.xml);
    and once the window opens view source and post the result.

    Thank you.

  • #5
    New to the CF scene
    Join Date
    Aug 2009
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    There are css divs with the names of the nodes in a HTML page.

    then as each Div[nodes] runs through the loop

    document.getElementById(Divs[nodes]).innerHTML=
    xmlDoc.getElementsByTagName(Divs[nodes])[0].childNodes[0].nodeValue+"<br>";

    the div nodes in the HTML page are updated by the document.getElementById

    *it outputs all the nodes until it reaches the one that is empty then stops, and throws the error: document.getElementById(Divs[nodes]) is null

    Code:
    <?php defined('SYSPATH') OR die('No direct access allowed.'); ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html>
    <head>
    
    <?php
    	echo html::stylesheet(array
    		(
    		 'assets/css/style'
    		),
    		array
    		(
    		 'screen'
    		), FALSE);
    ?>
    <script type="text/javascript" src="functions.js"></script>
    
    <script type="text/javascript">
    function make_blank()
    {
    document.search_box.search.value ="";
    }
    
    /*function hideResult(){
    document.getElementById('result').style.display = "none";
    }*/
    
    var xmlhttp;
    
    function doSearch()
    {
    xmlhttp=GetXmlHttpObject();
    if (xmlhttp==null)
      {
      alert ("Browser does not support HTTP Request");
      return;
      }
    
    
    
    var url="index.php/";
    url=url+"search?artist="+document.getElementById('search').value;
    ///url=url+"&sid="+Math.random();
    xmlhttp.onreadystatechange=stateChanged;
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
    }
    
    function stateChanged()
    {
    if (xmlhttp.readyState==4)
      {
      	
      	var Divs=new Array("artist","bio","img","date","tickets","venue","street","city","country","headliner");
    
    	xmlDoc=xmlhttp.responseXML;
    	
    	for  ( nodes in Divs )
    	{
    
    		if(!xmlDoc.getElementsByTagName(Divs[nodes])[0].childNodes[0].nodeValue)
    		{
    		}
    		else
    		{
    			document.getElementById(Divs[nodes]).innerHTML=
      			xmlDoc.getElementsByTagName(Divs[nodes])[0].childNodes[0].nodeValue+"<br>";
    		}
    
    	}
    	
    	/*document.getElementById("bio").innerHTML=
      	xmlDoc.getElementsByTagName("bio")[0].childNodes[0].nodeValue;
      	document.getElementById("headliner").innerHTML=
      	xmlDoc.getElementsByTagName("headliner")[0].childNodes[0].nodeValue;
    	
    	//document.getElementById("date").innerHTML=
      	//xmlDoc.getElementsByTagName("date")[0].childNodes[0].nodeValue;
    	//document.getElementById("tickets").innerHTML=
      	//xmlDoc.getElementsByTagName("tickets")[0].childNodes[0].nodeValue;
    	
      	document.getElementById("venue").innerHTML=
      	xmlDoc.getElementsByTagName("venue")[0].childNodes[0].nodeValue;
    	
    	
    	street=xmlDoc.getElementsByTagName("street")[0].childNodes[0].nodeValue;
    	if(street != null){
    		document.getElementById("street").innerHTML=street
      	}
    	
    	document.getElementById("city").innerHTML=
      	xmlDoc.getElementsByTagName("city")[0].childNodes[0].nodeValue;
    	document.getElementById("country").innerHTML=
      	xmlDoc.getElementsByTagName("country")[0].childNodes[0].nodeValue;
    	*/
    	
      
      }
    }
    
    function GetXmlHttpObject()
    {
    if (window.XMLHttpRequest)
      {
      // code for IE7+, Firefox, Chrome, Opera, Safari
      return new XMLHttpRequest();
      }
    if (window.ActiveXObject)
      {
      // code for IE6, IE5
      return new ActiveXObject("Microsoft.XMLHTTP");
      }
    return null;
    }
    var httpObject = null; 
    
    function handleEnter(inField, e) {
        var charCode;
        if(e && e.which){
            charCode = e.which;
        }else if(window.event){
            e = window.event;
            charCode = e.keyCode;
        }
    
        if(charCode == 13) {
            doSearch();
        }
    }
    </script>
    
    
    <title>CD COLLECTION</title>
    </head>
    
    </head>
    
    <body>
    <!--header paer start -->
    <div id="headerPan">
    
    <form name="search_box" onsubmit="return false;">
    <!--<label>search</label>-->
    <input type="text" id="search" name="search" value="Enter an artist name" onclick="make_blank();" onkeypress="handleEnter(this, event)"/>
    <input type="button" class="button" value="" title="button" onclick="doSearch();"/>
    
    </form>
    
    </div>
    <!--header part end -->
    <!--body part start -->
    <div id="mainBody">
    
    <div id=result>
    
    
    
    
    
    <span id="artist"></span> 		
    <span id="bio"></span> 			
    
    
    <!--Date: 		<span id="date"></span> 		<br />-->
    <span id="tickets"></span>		
    
    <span id="venue"></span>		
    <span id="headliner"></span>	
    <span id="street"></span>		
    <span id="city"></span>			
    <span id="country"></span>		
    </div>
    
    </div>
    <!--body part end -->
    <!--footer start -->
    
    <div id="footerMain">
    <div id="footer">
    <ul>
    </ul>
    
    </div>
    </div>
    <!--footer end -->
    </body>
    </html>
    hope that makes sense!

    cheers
    Last edited by boydt98; 08-10-2009 at 07:23 PM.

  • #6
    Senior Coder ckeyrouz's Avatar
    Join Date
    Jun 2009
    Location
    Montreal, Canada
    Posts
    1,044
    Thanks
    5
    Thanked 179 Times in 179 Posts
    after the following:
    Code:
    xmlDoc=xmlhttp.responseXML;
    write the following lines:
    Code:
    window.open().document.write(xmlhttp.responseXML.xml);
    alert(xmlhttp.responseXML.parseError.reason).
    alert(xmlhttp.responseXML.parseError.srcText).
    and send me the result please.

  • #7
    New to the CF scene
    Join Date
    Aug 2009
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    all 3 are undefined, seems as if theres no error from XML

    my guess is the XML file looks like this:

    <gig>
    <artist>tool</artist>
    <bio> blah blah blah </bio>
    <img></img>
    <date>123456789</date
    ...
    ...
    </gig>

    and the img node being empty is stopping the javascript for loop from continuing

    what do you think?

    cheers, tom.

  • #8
    Senior Coder ckeyrouz's Avatar
    Join Date
    Jun 2009
    Location
    Montreal, Canada
    Posts
    1,044
    Thanks
    5
    Thanked 179 Times in 179 Posts
    You got two problems in your code:
    1-You should replace this:
    Code:
    if(!xmlDoc.getElementsByTagName(Divs[nodes])[0].childNodes[0].nodeValue)
    with this:
    Code:
    if(!xmlDoc.getElementsByTagName(Divs[nodes])[0].childNodes[0])
    Means you should remove the nodeValue property access because the childNodes[0] is already null.

    2- you have a missing span (that of the date), it is commented in your code:
    so you should replace this:
    Code:
    <!--Date: 		<span id="date"></span> 		<br />-->
    with this:
    Code:
    <span id="date"></span>

  • Users who have thanked ckeyrouz for this post:

    boydt98 (08-10-2009)

  • #9
    New to the CF scene
    Join Date
    Aug 2009
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks for the help ckeyrouz! I was completely oblivious, I assumed the error was with retrieving the XML value and didn't actually read the error message correctly, Doh!

    I will carefully read error messages in the future from now on


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •