DOM error Messages

Having error messages working with XMl and PHP DOM

Warning: DOMDocument::createTextNode() expects parameter 1 to be string, object given in C:\xampplite\htdocs\dwax\processmeter.php on line 86

Catchable fatal error: Argument 1 passed to DOMNode::appendChild() must be an instance of DOMNode, null given in C:\xampplite\htdocs\dwax\processmeter.php on line 87
LVL 4
bobby101Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bobby101Author Commented:

<?php
session_start();
if (!$_SESSION['username']){
	header('Location: login.php');
}

	$date = $_POST['date'];
	$mnum = $_POST['mnum'];
	$read = $_POST['readings'];
	
	if ($date == "" && $mnum == "" && $read == ""){$error = "Unsuccessful";}
	else if (!$date == "" && !$mnum == "" && !$read == ""){
			//meter_readings.xml
			$xdoc = new DomDocument;
			$xdoc->Load('files/meter_readings.xml') or die('Unable to load file');
				$rootnode = $xdoc->getElementsByTagName('meters')->item(0);
				$dateE = $xdoc->createElement('date');
				//dates id attribute value <customer id=xxxx>
				$dteN = $xdoc->createAttribute('dt');
				$idValue = $xdoc->createTextNode($date);
				$dteN->appendChild($idValue);
				//<meters><dates>
				$rootnode->appendChild($dateE);
				//<meters><dtaes><date dt=xxxx>
				$dateE->appendChild($dteN);
				//<meterNumber>
				$mnE = $xdoc->createElement('meterNumber');
				//<meterNumber meternum=xxxxx reading=xxxx>
				$mnN = $xdoc->createAttribute('meternum');
				$mnValue = $xdoc->createTextNode($mnum);
				$mnN->appendChild($mnValue);
				//reading=xxxx
				$rN = $xdoc->createAttribute('reading');
				$rValue = $xdoc->createTextNode($read);
				$rN->appendChild($rValue);
				
				$dateE->appendChild($mnE);
				$mnE->appendChild($mnN);
				$mnE->appendChild($rN);
				
				//$xdoc->save('files/meter_readings.xml');
				//$error = "Successful";
				
				//customers xml
				$doc = new DomDocument;
				$doc->Load('files/customers.xml') or die('Unable to load file');
				$count = 0;
				    $node = $doc->getElementsByTagName('customer');
						foreach( $node as $find ){
							$customerMeters = $find->getElementsByTagName('meter');
							
								foreach($customerMeters as $customerMeter){
								$meter = $customerMeter->nodeValue;
							
									if ($mnum == $meter){
										$fnames = $doc->getElementsByTagName('firstName');
										$fname = $fnames->item($count)->nodeValue;
										$lnames = $doc->getElementsByTagName('lastName');
										$lname = $lnames->item($count)->nodeValue;	
										echo "Updating Meter reading and billing for <b>".$fname." ".$lname."</b>";
										// *************************
										$datesNodes = $find->getElementsByTagName('dates');
											foreach ($datesNodes as $datesNode) { 
											 $dates = $datesNode->getElementsByTagName('date'); 
												
													foreach ($dates as $date) { 
														$readings = $date->getElementsByTagName("reading")->item(0)->nodeValue;
													}
													
											}
										//***************************
										$lastReading = $readings;
										$newReading = $read - $lastReading;
										$charge = 0.124;
										$amount = $newReading * $charge;
										$gst = $amount * 0.1;
										$totalAmount = ($amount + $gst) + (0.373 * 30);
										echo "<br /><br />".$totalAmount;
										$dteNode = $find->getElementsByTagName('dates')->item(0);
										$d = $doc->createElement('date');
										
										// <date> SUBCHILDREN
												$dteNode->appendChild($d);
												// dt attribute dt=xxxxxx
													$dte = $doc->createAttribute('dt');
													$dteValue = $doc->createTextNode($date);
													$dte->appendChild($dteValue);
													
												$read = $doc->createElement('readings', $read);	
												$aDue = $doc->createElement('amtDue', $totalAmount);	
												$payDte = $doc->createElement('payDate');	
												$aPaid = $doc->createElement('amtPaid');	
												
													$d->appendChild($dte);
													$d->appendChild($read);
													$d->appendChild($aDue);
													$d->appendChild($payDte);
													$d->appendChild($aPaid);
										
										$doc->save('files/customers.xml');
										$error = "Successful";
										
									}// if meter == meternumber
								$count++;	
								}// end foreach meter
						
						}
						
						
	}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>Energetic Energy Management</title>
</head>
<body>
	<h3>Welcome, <?php echo $_SESSION['username']; ?></h3>
	<a href="logout.php">Logout</a>
	<p>&raquo; <a href="index.php">Home</a>

<h3>Meter Readings</h3>
<hr>
<form name="reading" method="post" action="">
<table cellspacing="3">
	<tr><td align="right">Date:</td><td><input type="text" name="date" size="20" /> DD/MM/YYYY</td></tr>
	<tr><td align="right">Meter Number:</td><td><input type="text" name="mnum" size="20" /></td></tr>
	<tr><td align="right">Meter Reading:</td><td><input type="text" name="readings" size="20" /></td></tr>
	<tr><td>&nbsp;</td><td><input type="submit" value="Add" name="add" /></td></tr>
	<tr><td>&nbsp;</td><td><p><span style="color:red;"><?php echo $error; ?></span></p></td></tr>
</table>
</form>

Open in new window

customers.xml
0
Julian HansenCommented:
Works for me - copied your code - created the two xml files and added

$error = "" ;

At the top of the file because it was not defined - created a new meters node in the meter_readings.xml

Posted my code below
<?php
function dump($obj)
{
	echo "<pre>";
	print_r($obj);
	echo "</pre>";
}
session_start();
$_SESSION['username'] = 'bob';
if (!$_SESSION['username']){
	header('Location: login.php');
}
	$error = "";
	$date = $_POST['date'];
	$mnum = $_POST['mnum'];
	$read = $_POST['readings'];
	
	if ($date == "" && $mnum == "" && $read == ""){$error = "Unsuccessful";}
	else if (!$date == "" && !$mnum == "" && !$read == ""){
			//meter_readings.xml
			$xdoc = new DomDocument;
			$xdoc->Load('files/meter_readings.xml') or die('Unable to load file');
				$rootnode = $xdoc->getElementsByTagName('meters')->item(0);
				dump($rootnode);
				$dateE = $xdoc->createElement('date');
				//dates id attribute value <customer id=xxxx>
				$dteN = $xdoc->createAttribute('dt');
				$idValue = $xdoc->createTextNode($date);
				$dteN->appendChild($idValue);
				//<meters><dates>
				$rootnode->appendChild($dateE);
				//<meters><dtaes><date dt=xxxx>
				$dateE->appendChild($dteN);
				//<meterNumber>
				$mnE = $xdoc->createElement('meterNumber');
				//<meterNumber meternum=xxxxx reading=xxxx>
				$mnN = $xdoc->createAttribute('meternum');
				$mnValue = $xdoc->createTextNode($mnum);
				$mnN->appendChild($mnValue);
				//reading=xxxx
				$rN = $xdoc->createAttribute('reading');
				$rValue = $xdoc->createTextNode($read);
				$rN->appendChild($rValue);
				
				$dateE->appendChild($mnE);
				$mnE->appendChild($mnN);
				$mnE->appendChild($rN);
				
				$xdoc->save('files/meter_readings.xml');
				$error = "Successful";
				
				//customers xml
				$doc = new DomDocument;
				$doc->Load('files/customers.xml') or die('Unable to load file');
				$count = 0;
				    $node = $doc->getElementsByTagName('customer');
						foreach( $node as $find ){
							$customerMeters = $find->getElementsByTagName('meter');
							
								foreach($customerMeters as $customerMeter){
								$meter = $customerMeter->nodeValue;
							
									if ($mnum == $meter){
										$fnames = $doc->getElementsByTagName('firstName');
										$fname = $fnames->item($count)->nodeValue;
										$lnames = $doc->getElementsByTagName('lastName');
										$lname = $lnames->item($count)->nodeValue;	
										echo "Updating Meter reading and billing for <b>".$fname." ".$lname."</b>";
										// *************************
										$datesNodes = $find->getElementsByTagName('dates');
											foreach ($datesNodes as $datesNode) { 
											 $dates = $datesNode->getElementsByTagName('date'); 
												
													foreach ($dates as $date) { 
														$readings = $date->getElementsByTagName("reading")->item(0)->nodeValue;
													}
													
											}
										//***************************
										$lastReading = $readings;
										$newReading = $read - $lastReading;
										$charge = 0.124;
										$amount = $newReading * $charge;
										$gst = $amount * 0.1;
										$totalAmount = ($amount + $gst) + (0.373 * 30);
										echo "<br /><br />".$totalAmount;
										$dteNode = $find->getElementsByTagName('dates')->item(0);
										$d = $doc->createElement('date');
										
										// <date> SUBCHILDREN
												$dteNode->appendChild($d);
												// dt attribute dt=xxxxxx
													$dte = $doc->createAttribute('dt');
													$dteValue = $doc->createTextNode($date);
													$dte->appendChild($dteValue);
													
												$read = $doc->createElement('readings', $read);	
												$aDue = $doc->createElement('amtDue', $totalAmount);	
												$payDte = $doc->createElement('payDate');	
												$aPaid = $doc->createElement('amtPaid');	
												
													$d->appendChild($dte);
													$d->appendChild($read);
													$d->appendChild($aDue);
													$d->appendChild($payDte);
													$d->appendChild($aPaid);
										
										$doc->save('files/customers.xml');
										$error = "Successful";
										
									}// if meter == meternumber
								$count++;	
								}// end foreach meter
						
						}
						
						
	}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>Energetic Energy Management</title>
</head>
<body>
	<h3>Welcome, <?php echo $_SESSION['username']; ?></h3>
	<a href="logout.php">Logout</a>
	<p>&raquo; <a href="index.php">Home</a>

<h3>Meter Readings</h3>
<hr>
<form name="reading" method="post" action="">
<table cellspacing="3">
	<tr><td align="right">Date:</td><td><input type="text" name="date" size="20" /> DD/MM/YYYY</td></tr>
	<tr><td align="right">Meter Number:</td><td><input type="text" name="mnum" size="20" /></td></tr>
	<tr><td align="right">Meter Reading:</td><td><input type="text" name="readings" size="20" /></td></tr>
	<tr><td>&nbsp;</td><td><input type="submit" value="Add" name="add" /></td></tr>
	<tr><td>&nbsp;</td><td><p><span style="color:red;"><?php echo $error; ?></span></p></td></tr>
</table>
</form>

Open in new window

0
Julian HansenCommented:
Ok - sorry - did not download the customers.xml file - so was not getting the error.

Found your problem.

you are looping through

$dates = $datesNode->getElementsByTagName('date');
                                                                        
      foreach ($dates as $date) {
      $readings = $date->getElementsByTagName("reading")->item(0)->nodeValue;
        }
                                                                              }
                                                                              
                        
0
Julian HansenCommented:
Sorry - submit by mistake

Ok - sorry - did not download the customers.xml file - so was not getting the error.

Found your problem. (Line 66)

you are looping through the date nodes and overwriting the $date variable with $date in the loop. Change the $date in the loop to $d or something and it works - see below

$dates = $datesNode->getElementsByTagName('date');
                                                                       
      foreach ($dates as $d) {   <---- Change this on line 66
      $readings = $date->getElementsByTagName("reading")->item(0)->nodeValue;
        }
                                                                              }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bobby101Author Commented:
THANKS A++++++ EXCELLENT!!!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Components

From novice to tech pro — start learning today.