...

View Full Version : Checking XML node for null or undefined



boydt98
08-10-2009, 06:06 PM
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.

ckeyrouz
08-10-2009, 06:08 PM
Can you please you xmlDoc coming from the following :

xmlDoc=xmlhttp.responseXML;

in order to track the problem?

boydt98
08-10-2009, 06:11 PM
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

ckeyrouz
08-10-2009, 07:07 PM
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:

window.open().document.write(xmlhttp.responseXML.xml);
and once the window opens view source and post the result.

Thank you.

boydt98
08-10-2009, 07:18 PM
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


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

ckeyrouz
08-10-2009, 07:30 PM
after the following:

xmlDoc=xmlhttp.responseXML;


write the following lines:

window.open().document.write(xmlhttp.responseXML.xml);
alert(xmlhttp.responseXML.parseError.reason).
alert(xmlhttp.responseXML.parseError.srcText).


and send me the result please.

boydt98
08-10-2009, 07:36 PM
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.

ckeyrouz
08-10-2009, 08:03 PM
You got two problems in your code:
1-You should replace this:

if(!xmlDoc.getElementsByTagName(Divs[nodes])[0].childNodes[0].nodeValue)


with this:

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:

<!--Date: <span id="date"></span> <br />-->


with this:

<span id="date"></span>

boydt98
08-10-2009, 11:25 PM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum