Hello,

i am a newbie on php programming and try working on my last project in my school. My program is to compare 2 different xml file, it working well at first. but when i tried to add more xml file to compare, the page keeps telling "Fatal error: Call to a member function hasChildNodes() on a non-object" i dont know why :(

anyone has an idea why this message keeps appear? thank you.. your comment is much appreciated.

this is the error :

Fatal error: Call to a member function hasChildNodes() on a non-object in C:\xampp\htdocs\new_6jan.php on line 46

this is the code of new_6jan.php

<?
//bikin radio button buat milih nama file produk yang mau di compare
error_reporting(E_ALL ^ E_NOTICE);
$xml1="spreadrum_g189.xml";
$xml2="xml_softspec.xml";
$xml3="mtk_p800_1.xml";

$dom1= new DOMDocument;
$dom1->load($xml1); 
$dom1->formatOutput=true;
$dom1->preserveWhiteSpace = false;
$dom1->normalizeDocument();

$dom2= new DOMDocument;
$dom2->load($xml2);
$dom2->formatOutput=true;
$dom2->preserveWhiteSpace = false;
$dom2->normalizeDocument();

$dom3= new DOMDocument;
$dom3->load($xml3); 
$dom3->formatOutput=true;
$dom3->preserveWhiteSpace = false;
$dom3->normalizeDocument();

$sspec1=$dom1->getElementsByTagName("soft_spec")->item('0');
$sspec1->normalize();
$sspec2=$dom2->getElementsByTagName("soft_spec")->item('0');
$sspec2->normalize();
$sspec3=$dom3->getElementsByTagName("soft_spec")->item('0');
$sspec3->normalize();


$node=0;
$node1=0;
$match=0;
$match_mtk=0;
$unmatch=0;
$unmatch_mtk=0;
$mp=0; 
$count=0; 
$countdb=0;


function hasChild($hc){
		if($hc->hasChildNodes()){
			foreach ($hc->childNodes as $c){
			if($c->nodeType == XML_ELEMENT_NODE)
			return true;
			};
		}
		return false;
		
	};

function shownode($x){
		foreach($x->childNodes as $p){
		if (hasChild($p)){
			echo $p->nodeName . " ->HAS CHILD <br> ";
			shownode($p);
			} elseif ($p->nodeType == XML_ELEMENT_NODE)
			echo $p->nodeName . " " . $p->nodeValue . '<br>';
		};
};


include "child1.php";
include "sub_child12.php";
include "ret_dbdata.php";
include "ret_reqdata.php";
//include "count_req.php"; belum solved. ntar si softspec out aja ditambahin kondisi.
//soalnya yang gak punya anak juga diitung isinya requirementnya.
include "count_sped.php";
//include "ret_dbmtk.php";
include "matching_child1.php";
//include "matching_child_mtk.php";

//include "saving_result.php";  

echo "Spreadrum Match   : " . $match ."<br>";
echo "Mtk P800 Match    : " . $match_mtk ."<br>";
echo "Requirement : " . $count ."<br>";
echo "Product Specification Spreadrum: " . $count_sped ."<br>";
echo "Product Specification MTK: " . $count_mtk ."<br>";

if($match > $count){
echo "Invalid Input... <br>";
echo "Please Try again";
//masukin button back yang redirect ke tempat input
}
else{
$mp_sped=($match/$count_sped)*100;
$mp_mtk=($match_mtk/$count_mtk)*100;
};

//include "grafik_bar.php";
?>
<html>
<img src="grafik_bar.php?sped=<?$match?>">
</html>
<?
echo "<br>" . "<br>";
echo " Total Match Percentage Spreadrum : " . $mp_sped . "%" . "<br>";
echo " Total Match Percentage MTK	    : " . $mp_mtk . "%";
?>

and i after i do some debugging, i think the problem is relies on file ret_dbmtk.php,
include matching_child1.php, and matching_child_mtk.php

here goes the code for matching_child1.php

<?
if(!function_exists('hasChild')){
	function hasChild($hc){
		if($hc->hasChildNodes()){
			foreach ($hc->childNodes as $c){
			if($c->nodeType == XML_ELEMENT_NODE)
			return true;
			};
		return false;
		};
	};
};

foreach($child_2 as $y){
	$m1=$dom1->getElementsByTagName($y)->item('1');
		if ($m1 == NULL){
		$m1=$dom1->getElementsByTagName($y)->item('0');
		};
	$m2=$dom2->getElementsByTagName($y)->item('0');
		if ($m2 == NULL){
		$m2=$dom2->getElementsByTagName($y)->item('1');
		};
//data_con
	if(hasChild($m2)){
		foreach($m2->childNodes as $a){
		$ca = $a->nodeName;
		//ca isinya sms, dll
			if ($ca != "#text"){
			//echo $ca . "<br>";
			$mc1=$dom1->getElementsByTagName($ca)->item('1');
			if ($mc1 == NULL){
			$mc1=$dom1->getElementsByTagName($ca)->item('0');
			};
				if(hasChild($a)){
					foreach($a->childNodes as $ac){
					//y,caa isinya anaknya mwr,dll 
						$anak3=$ac->nodeName;
						if($anak3 != "#text"){
							$mcc1=$dom1->getElementsByTagName($anak3)->item('1');
								if ($mcc1 == NULL){
								$mcc1=$dom1->getElementsByTagName($anak3)->item('0');
								};
							$mcc2=$dom2->getElementsByTagName($anak3)->item('0');
								if ($mcc2 == NULL){
								$mcc2=$dom2->getElementsByTagName($anak3)->item('1');
								};
					//echo $anak3 . "<br>";
							$ibu31=$mc1->nodeName; $ibu32=$a->nodeName;
							if($ibu31 == $ibu32 ){
										if($varr1[$ibu31][$anak3] == $varr2[$ibu32][$anak3] && ($varr2[$ibu32][$anak3]!= Null || $varr2[$ibu32][$anak3]!= "")){
										//echo $ibu31 ."->".$anak3 ." " . $varr1[$ibu31][$anak3] ."<br>";
										$match=$match+1;							
										}
										else{
										$unmatch=$unmatch+1;
										};
										
									};
							};
						};
					}
				else{
				$elemn1=$m1->nodeName; $elemn2=$m2->nodeName;
					if($elemn1 == $elemn2){
					//echo $elemn1 . " " . $elemn2 . "<br>";
					//echo $ca . " 1 " . $nc1[$elemn1][$ca] . " 2 " . $nc2[$elemn2][$ca] ."<br>";
						if($nc1[$elemn1][$ca] == $nc2[$elemn2][$ca] && ($nc1[$elemn1][$ca]!= Null || $nc1[$elemn1][$ca]!= "")){
						//echo $elemn1 . "  " . $ca . "->" . $nc1[$elemn1][$ca] . "<br>";
						$match=$match+1;
						}
						else{
						$unmatch=$unmatch+1;
						};
					};
				};
			};
		};
	}
	else{
	//ngambil anaknya tools yang gak punya anak (kaya chat, clock,dll)
	$elemn1=$m1->nodeName; $elemn2=$m2->nodeName;
		if ($elemn2 == $elemn1)/*bs gak dipake*/{
			if ($pn1[$elemn1] == $pn2[$elemn2] && ($pn2[$elemn2]!= Null || $pn2[$elemn2]!= "")){
			//echo $elemn1 . " ". $pn1[$elemn1] ."<br>"  ;
			$match=$match+1;
			}
			else { 
			$umatch=$umatch+1;
			};
		};	
	};
};
 

?>

and this for ret_dbmtk.php. (actually the ret_dbmtk.php is identical code with ret_dbdata.php and ret_reqdata.php except their loading different xml file . it working well on both file, but on this one, it keeps telling me the error that i said above :( )

<?
foreach($child_2 as $y){
	$child32=$dom3->getElementsByTagName($y)->item('1');
	if ($child32 == NULL){
	$child32=$dom3->getElementsByTagName($y)->item('0');
	};
	//data_con
//echo $y. " ";
	if(hasChild($child32)){
			$a=$child32->nodeName;
				//anaknya data_con/message -> sms,mms,dll.
				foreach($child32->childNodes as $c1){
					$search = $c1->nodeName;
					//echo $search;
					$c31=$dom3->getElementsByTagName($search)->item('1');
							if ($c31 == NULL){
							$c31=$dom3->getElementsByTagName($search)->item('0');
							};	
					if($c1->nodeName != "#text"){
					//c1 isinya gprs, mms,dll
					//echo $search . " ";
						if (hasChild($c31)){
						foreach ($c1->childNodes as $ccv){
							if($ccv->nodeName !="#text"){
								$ibu1=$c1->nodeName;
								$anak1 = $ccv->nodeName;
								$varr3[$ibu1][$anak1] = $ccv->nodeValue;
								//echo $ibu1 . " -> " . $anak1 . $varr3[$ibu1][$anak1] . "<br>";
								$count_mtk=$count_mtk+1;
								};
							};
						}
						else{
							if($search != "#text" ){
							$nc3[$child32->nodeName][$search] = $c31->nodeValue;
							//echo $child32->nodeName . $c1->nodeName . " -> " . $nc3[$child32->nodeName][$c1->nodeName] ."<br>";	
							$count_mtk=$count_mtk+1;
							};
						};
					};
				};
			}
			else {
				if($child32->nodeName != "#text"){
				$pn3[$child32->nodeName] = $child32->nodeValue;
				//echo $child32->nodeName . " -> " . $pn3[$child32->nodeName] ."<br>";
				$count_mtk=$count_mtk+1;
				}
			
			};
		
}; 
?>

anyone can help me? :)

Recommended Answers

I think it is best if you change your function to this:

function hasChild($hc)
{
  if (($hc != null) && $hc->hasChildNodes())
  {
    foreach ($hc->childNodes as $c)
    {
      if ($c->nodeType == XML_ELEMENT_NODE)
        return true;
    }
  }
  return false;
}

I added the null check, because somewhere in your …

Jump to Post

All 2 Replies

I think it is best if you change your function to this:

function hasChild($hc)
{
  if (($hc != null) && $hc->hasChildNodes())
  {
    foreach ($hc->childNodes as $c)
    {
      if ($c->nodeType == XML_ELEMENT_NODE)
        return true;
    }
  }
  return false;
}

I added the null check, because somewhere in your code this function is called on a null object. This is an easy way to avoid such errors, and returning false will allow normal application flow.

oh my! it works!!! Much Much appreciated! thank you so much for your help :')

Be a part of the DaniWeb community

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