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.


		<TITLE>Empire Burlesque</TITLE> 
		<ARTIST>Bob Dylan</ARTIST> 

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.


<script language="javascript" type="text/javascript">
function showCD(str){
	if (str=="")
	if (window.XMLHttpRequest)
		xmlhttp=new XMLHttpRequest();
		xmlhttp=new activeXObject("Microsoft.XMLHTTP");
		if (xmlhttp.readyState==4 && xmlhttp.status==200)


<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>
<div id="txtHint"><b>CD info will be listed here</b></div>



$xmlDoc=new DOMDocument();


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

Recommended Answers

All 3 Replies

I think you meant nextSibling() , not next_sibling() :

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:

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:


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

	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 
		//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, learning, and sharing knowledge.