Hi,

I'm getting the following error when i try to get the next sibling of the current node in php:

Fatal error: Call to undefined method DOMElement::next_sibling() in C:\wamp\www\Ajax\getcd.php on line 1

does anyone know on what i'm doing wrong? thank you.

cd_catalog.xml

<CATALOG> 
	<CD> 
		<TITLE>Empire Burlesque</TITLE> 
		<ARTIST>Bob Dylan</ARTIST> 
		<COUNTRY>USA</COUNTRY> 
		<COMPANY>Columbia</COMPANY> 
		<PRICE>10.90</PRICE> 
		<YEAR>1985</YEAR> 
	</CD> 
       </CATALOG>

if the user selects Bob Dylan from the select box in the following getcd.html, i want the next sibling ie. COUNTRY: USA to display.

getcd.html

<script language="javascript" type="text/javascript">
function showCD(str){
	if (str=="")
		{
			document.getElementById("txtHint").innerHTML="";
			return;
		}
	if (window.XMLHttpRequest)
		xmlhttp=new XMLHttpRequest();
	else
		xmlhttp=new activeXObject("Microsoft.XMLHTTP");
		
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
			document.getElementById("txtHint").innerHTML=xmlhttp.responseText
	}
	
	xmlhttp.open("GET","getcd.php?q="+str,true);
	xmlhttp.send();
}
</script>

</head>

<body>
<form action="">
<select name="cds" onchange="showCD(this.value)">
<option value="">Select a CD</option>
<option value="Bob Dylan">Bob Dylan</option>
<option value="Bonnie Tyler">Bonnie Tyler</option>
<option value="Dolly Parton">Dolly Parton</option>
</select>
</form>
<div id="txtHint"><b>CD info will be listed here</b></div>
</body>
</html>

getcd.php

<?php
$q=$_GET['q'];

$xmlDoc=new DOMDocument();
$xmlDoc->load("cd_catalog.xml");

$x=$xmlDoc->getElementsByTagName('ARTIST');

for($i=0;$i<=$x->length-1;$i++)
	{
	//process only element nodes
	if ($x->item($i)->nodeType==1)
	  {
	   if ($x->item($i)->childNodes->item(0)->nodeValue==$q)
	      {	echo $x->item($i)->next_sibling(0)->nodeName.":";
		echo $x->item($i)->next_sibling(0)->item(0)->nodeValue;
	      }
	  }	
         }
?>

I think you meant nextSibling() , not next_sibling() :
http://www.php.net/manual/en/class.domnode.php

After changing to nextSibling, i'm still getting the same error.
Fatal error: Call to undefined method DOMElement::nextSibling()

thank you.

FYI: $x->item($i) is a DOMNode object. Be sure to read the manual on the DOMNode to see what properties it supports:
http://www.php.net/manual/en/class.domnode.php

After you have read at least the properties of the DOMNode, read the comments in the code and notice how I've used the properties of the DOMNode to reference the node values that you want. Within the loop, uncomment the line that you want to try:

$x=$xmlDoc->getElementsByTagName('ARTIST');

for($i=0,$limit=$x->length; $i<$limit; ++$i)
{
	//this $n is just to have a convenient shortcut in the code that follows
	$n=$x->item($i);

	if( $n->nodeValue==$q )
	{
		//this will give you the COUNTRY node regardless of the order 
		//in which it appears within the CD tag. You can of course use
		//the same code/line for other tags, not just COUNTRY. This is
		//convenient when someone else will be maintaining/providing 
		//the xml and you don't know for sure they'll be providing the
		//the nodes in the specific order in which you expect them
		//echo $n->parentNode->getElementsByTagName('COUNTRY')->item(0)->nodeValue;
		
		//The lines that follow show you how to achieve what you are after
		//but unlike the example above, they are dependent on a specific
		//order of the nodes.

		//This will give you the COUNTRY node only when it appears
		//immediately after the ARTIST node within the CD tag
		//echo $n->nextSibling->nodeValue;

		//you can chain nextSibling to get to other nodes located 
		//AFTER COUNTRY
		//ex:
		//to get COMPANY
		//echo $n->nextSibling->nextSibling->nodeValue;
		//
		//to get PRICE
		//echo $n->nextSibling->nextSibling->nextSibling->nodeValue;
	}
}

On another note, if you want to make the search case-insensitive (ex: if user type ' BOB DYLan' and you still want it to match 'Bob Dylan', then instead of if( $n->nodeValue==$q ) use if( trim(strtolower($n->nodeValue))==trim(strtolower($q)) )

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.