Looping through XML file

Having problem loop through nodes
LVL 4
bobby101Asked:
Who is Participating?
 
bobby101Author Commented:
XML CODE

<customers>
      <customer id="000">
            <meter>134456</meter>
            <name>
                  <title/>
                  <firstName>Bart</firstName>
                  <middleName/>
                  <lastName>Simpson</lastName>
            </name>
            <address>
                  <street>45 Olive Dr</street>
                  <city>Sydney</city>
                  <postcode>2000</postcode>
                  <state>NSW</state>
            </address>
            <phone>
                  <home>0298576433</home>
                  <mobile>0413335678</mobile>
                  <work>0296784455</work>
            </phone>
            <dates>
                  <date dt="29/11/2008">
                        <reading>62990</reading>
                        <amtDue>0.00</amtDue>
                        <payDate>new acc</payDate>
                        <amtPaid> </amtPaid>
                  </date>
                  <date dt="28/02/2009">
                        <reading>64492</reading>
                        <amtDue>242.21</amtDue>
                        <payDate>30/03/2009</payDate>
                        <amtPaid>242.21</amtPaid>
                  </date>
                  <date dt="29/05/2009">
                        <reading>65374</reading>
                        <amtDue>157.23</amtDue>
                        <payDate>24/06/2009</payDate>
                        <amtPaid>157.23</amtPaid>
                  </date>
                  <date dt="29/08/2009">
                        <reading>66348</reading>
                        <amtDue>170.60</amtDue>
                        <payDate>19/09/2009</payDate>
                        <amtPaid>170.60</amtPaid>
                  </date>
            </dates>
      </customer>
</customers>


GET AND ERROR MESSAGE:
Fatal error: Call to a member function getAttribute() on a non-object in C:\xampp\htdocs\dwax\billpayment.php on line 24

<?PHP 
session_start();
if (!$_SESSION['username']){
	header('Location: login.php');
}
$error = "";
	$target = $_POST['target']; 
	if(!$target == ""){
		$count = 0;
        $file = "files/customers.xml"; 
		  
			  $doc = new DOMDocument();
			  $doc->load($file) or die('Unable to load file');
			  
			  $node = $doc->getElementsByTagName('customer');
			  foreach( $node as $find ){
			  $id = $find->getAttribute('id');
			 
					  if ($target == $id){
						$num = 0;
						$d = $doc->getElementsByTagName('dates')->item($count);
						while ($d){
						   $date = $d->getElementsByTagName('date')->item($num);
						   $dt = $date->getAttribute('dt');
						   echo $dt."<br />";
						   $num++; 
						}
						
					  }
				$count++;
			  }
	}
	//else if ($target == ""){$error="Unsuccessful";}	
		
?>
<!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>

<form name="search" method="post" action=""> 
<h3>Bill Payment</h3>
<p>Customer ID: <input type="text" name="target" size="20" /></p>
<input type="submit" value="Go" name="search" />
</form></p> 
<p><span style="color:red;"><?php echo $error; ?></span></p>

Open in new window

0
 
Avinash ZalaWeb ExpertCommented:
Try attached code with your xml file, change the xml file name accordingly.

Hope this helps,
Addy
<?php 
    $webroot="http://".$_SERVER['HTTP_HOST']."/";
    $doc = new DOMDocument();

    $doc->load( $webroot.'/demo.xml' );

    $note = $doc->getElementsByTagName( "customer" );
    $final_arr = array();
    $i=0;
    foreach($note as $note)
    {
		$id = $note->getAttribute('id');

		$dates = $note->getElementsByTagName("dates");
	
        foreach($dates as $dates)
        {
			$date = $dates->getElementsByTagName("date");
			foreach($date as $date)
			{
				echo  $dated= $date->getAttribute('dt');
				 echo "<br/>";
			}
        }
        $i++;
    }
?>

Open in new window

0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
bobby101Author Commented:
Sorry didnt work i its bring up the dates from other customer/s nodes as well.
basically how it should work is that you type in the customer id and brings up the billing date history of that customer i had it working but it also gives a error:
Fatal error: Call to a member function getAttribute() on a non-object in C:\xampp\htdocs\dwax\billpayment.php on line 24

0
 
bobby101Author Commented:
output on browser:
29/11/2008
28/02/2009
29/05/2009
29/08/2009

Fatal error: Call to a member function getAttribute() on a non-object in C:\xampplite\htdocs\dwax\billpayment.php on line 24
0
 
Avinash ZalaWeb ExpertCommented:
i am not getting any error at all.

and which dates you want to get?


Addy
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.