Searching by Attributes XML

hi im trying to search by Attribute of an 'id' from an xml
LVL 4
bobby101Asked:
Who is Participating?
 
Avinash ZalaWeb ExpertCommented:
HI

try attached code:


hope this helps,
Addy
<?php 
	$dom=new DOMDocument();
	$dom->load('xml file webpath');
	
	$root=$dom->documentElement; // This can differ (I am not sure, it can be only documentElement or documentElement->firstChild or only firstChild)
	
	foreach ($root->childNodes as $marker) 
	{
		$type=$marker->getAttribute('id');
		
		echo $type;
	}
?>

Open in new window

0
 
bobby101Author Commented:
XML CODE:
<customers>
      <customer id="BL343456">
            <meter>134456</meter>
            <name>
                  <title/>
                  <firstName>Bart</firstName>
                  <middleName/>
                  <lastName>Simpson</lastName>
            </name>
</customer>
</customers>
<?PHP 
session_start();
if (!$_SESSION['username']){
	header('Location: login.php');
}

	$target = $_POST['target']; 
	if(!$target == ""){
		$error = "";
        $count = 0;
        $file = "files/customers.xml"; 
		  
			  $doc = new DOMDocument();
			  $doc->load( $file) or die('Unable to load file');
			  
			  $element = $doc->getElementsByTagName( 'customer' );
			  $node = $element->getAttributeNode('id');
			  foreach( $node as $find ){
				$id = $find->nodeValue;
					  if ($target == $id){
					   echo 'hello';
					  }
					  else{$error = "Not Found";}
				$count++;
			  }
	}		  
?>
<!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
 
StealthyDevCommented:
Hi, change your code like this:



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

	$target = $_POST['target']; 
	if(!$target == ""){
		$error = "";
        $count = 0;
        $file = "files/customers.xml"; 
		  
			  $doc = new DOMDocument();
			  $doc->load( $file) or die('Unable to load file');
			  
			  $element = $doc->getElementsByTagName( 'customer' );
			  $node = $element->getAttribute('id');
			  foreach( $node as $find ){
				$id = $find->nodeValue;
					  if ($target == $id){
					   echo 'hello';
					  }
					  else{$error = "Not Found";}
				$count++;
			  }
	}		  
?>
<!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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
bobby101Author Commented:
Srry didnt work i get an error message:
 Call to undefined method DOMText::getAttribute()

senthurpandian: >> code was no different to mine
0
 
bobby101Author Commented:
this works
Thank for your help
$node = $doc->getElementsByTagName('customer');
			  foreach( $node as $find ){
			  $id = $find->getAttribute('id');
			 
					  if ($target == $id){
					   $doc->getElementsByTagName( 'firstName' )->item($count)->nodeValue;
					  }
				$count++;
			  }

Open in new window

0
 
bobby101Author Commented:
ok still having problem with attributes so far i managed to get the search working but when i want to display a value of an attribute i get error message
----Call to undefined function getattribute()
     $doc->getElementsByTagName( 'date' )->item($count)->getAttribute('dt');

xml code
<date dt="27/11/2008" />
0
 
bobby101Author Commented:
Did not solve the actual problem but useful
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.