Searching by Attributes XML

hi im trying to search by Attribute of an 'id' from an xml
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:
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
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

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
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
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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
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.