Deleting data from XML doc using PHP

I am using the code below to delete data from a XML document. One block of code looks like:

            <Employee Status="Part-time">
                  <Name>Alice Ashman</Name>
                  <Position>Administrative Assistant</Position>
                  <Phone>555-4580 ext. 581</Phone>
                  <Years>5</Years>
            </Employee>

I have been using this as a resource: http://us3.php.net/manual/en/ref.domxml.php

I am trying to use the <Name> to tell which to delete. But I get these errors:


Warning: domdocument() expects at least 1 parameter, 0 given in /Users/ryancoughlin/Sites/xml_final/delete.php on line 13

Fatal error: Call to undefined function: load() in /Users/ryancoughlin/Sites/xml_final/delete.php on line 14

Any ideas? I am running PHP 4 locally using MAMP.

Thanks!

Ryan


<?php 
error_reporting(E_ALL);
include ("config.inc.php");
 if(isset($_GET['delete'])){
	$query = 'DELETE FROM tblemp WHERE name LIKE "'.$_GET['delete'].'%"';
	mysql_query($query) or die(mysql_error());
}
//header('Location: http://localhost:8888/xml_final/deleted.html');
?>

<?php

$doc = new DOMDocument; 
$doc->load('All_emp2.xml');

$thedocument = $doc->documentElement;

//this gives you a list of the messages
$list = $thedocument->getElementsByTagName('Employee');

//figure out which ones you want -- assign it to a variable (ie: $nodeToRemove )
$nodeToRemove = null;
foreach ($list as $domElement){
  //$attrValue = $domElement->getAttribute('time');
  $attrValue = $domElement->getElementsByTagName('Name');
  if ($attrValue == $_GET['delete']) {
    $nodeToRemove = $domElement; //will only remember last one- but this is just an example :)
  }
}

//Now remove it.
if ($nodeToRemove != null)
$thedocument->removeChild($nodeToRemove);

echo $doc->saveXML(); 
?>

Open in new window

LVL 1
catonthecouchproductionsAsked:
Who is Participating?
 
TiberiuGalCommented:
ps.
I believe you are looking to delete an employee with a given name, so your loop should be this:

foreach ($list as $domElement){
  $attrValue = $domElement->getElementsByTagName('Name');
  if ($attrValue->length && 
       $attrValue->items(0)->textContent == $_GET['delete']) {
    $nodeToRemove = $domElement; 
  }
}

Open in new window

0
 
hieloCommented:
on the link you provided, where do you see that load() is a defined method?

The error clearly tells you that there is no such thing. Refer to:
domxml_open_file

http://us3.php.net/manual/en/function.domxml-open-file.php
0
 
TiberiuGalCommented:
Hi,
just as hielo pointed out, you are using the php5 DOM functionality on php4. It won't work
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
catonthecouchproductionsAuthor Commented:
Hey guys,

I switched over to PHP4 for this. Now when I run it, it outputs the XML, but I still see that result I am trying to delete in the XML.

Any ideas?

Is it because its not catching the name tag? I have it looking for Employee first, then I need to get in to the child element Name, then use that to tell which to delete. I think it is close.

Thanks again,

Ryan
//this gives you a list of the messages
$list = $thedocument->getElementsByTagName('Employee');

//figure out which ones you want -- assign it to a variable (ie: $nodeToRemove )
$nodeToRemove = null;
foreach ($list as $domElement){
  //$attrValue = $domElement->getAttribute('time');
  $attrValue = $domElement->getElementsByTagName('Name');
  if ($attrValue == $_GET['delete']) {
    $nodeToRemove = $domElement; //will only remember last one- but this is just an example :)
  }
}

Open in new window

0
 
catonthecouchproductionsAuthor Commented:
I mean, I went to PHP5, sorry.

Ryan
0
 
TiberiuGalCommented:
hi,
change your loop like this:

foreach ($list as $domElement){
  $attrValue = $domElement->getElementsByTagName('Name');
  if ($attrValue->length && 
       $attrValue->items(0)->nodeName ==$_GET['delete']) {
    $nodeToRemove = $domElement; 
  }
}

Open in new window

0
 
catonthecouchproductionsAuthor Commented:
Hey,

Thank you, i tried that and it threw an error at line 26:

       $attrValue->items(0)->nodeName ==$_GET['delete']) {

Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /Users/ryancoughlin/Sites/xml_final/delete.php on line 26

Any ideas?

Thank you,

Ryan
0
 
TiberiuGalCommented:
sorry, It's
$attrValue->item(0)->textContent
item not items
0
 
catonthecouchproductionsAuthor Commented:
Hmmm not sure why it is doing that, still erroring, this is what I am sending to the server:

delete.php?delete=Alice Ashman

That is with your latest post changing items to item, below is my XML I am using.
<?xml version="1.0"?>
<Staff>
		<Employee Status="Part-time">
			<Name>Alice Ashman</Name>
			<Position>Administrative Assistant</Position>
			<Phone>555-4580 ext. 581</Phone>
			<Years>5</Years>
		</Employee>
		<Employee Status="Full-time">
			<Name>Tom Forrest</Name>
			<Position>Accounts Manager</Position>
			<Phone>555-4580 ext. 582</Phone>
			<Years>14</Years>
		</Employee>
		<Employee Status="Part-time">
			<Name>Diane Kerkman</Name>
			<Position>Administrative Assistant</Position>
			<Phone>555-4580 ext. 473</Phone>
			<Years>8</Years>
		</Employee>
		<Employee Status="Full-time">
			<Name>Mary Oneil</Name>
			<Position>Accounts Manager</Position>
			<Phone>555-4580 ext. 619</Phone>
			<Years>11</Years>
		</Employee>
		<Employee Status="Full-time">
			<Name>Donald Watkins</Name>
			<Position>Vice President</Position>
			<Phone>555-4580 ext. 243</Phone>
			<Years>15</Years>
		</Employee>
		<Employee Status="Full-time">
			<Name>Jeff Bester</Name>
			<Position>Sales Manager</Position>
			<Phone>555-4580 ext. 411</Phone>
			<Years>3</Years>
		</Employee>
		<Employee Status="Part-time">
			<Name>Pete DuMont</Name>
			<Position>Salesperson</Position>
			<Phone>555-4580 ext. 990</Phone>
			<Years>1</Years>
		</Employee>
		<Employee Status="Full-time">
			<Name>Linda Howard</Name>
			<Position>Sales Coordinator</Position>
			<Phone>555-4580 ext. 518</Phone>
			<Years>3</Years>
		</Employee>
		<Employee Status="Part-time">
			<Name>Julie Nelson</Name>
			<Position>Sales Assistant</Position>
			<Phone>555-4580 ext. 610</Phone>
			<Years>1</Years>
		</Employee>
		<Employee Status="Full-time">
			<Name>Maria Pauley</Name>
			<Position>Salesperson</Position>
			<Phone>555-4580 ext. 611</Phone>
			<Years>2</Years>
		</Employee>
		<Employee Status="Full-time">
			<Name>Paul Staford</Name>
			<Position>Salesperson</Position>
			<Phone>555-4580 ext. 319</Phone>
			<Years>6</Years>
		</Employee>
		<Employee Status="Full-time">
			<Name>Shirley Winston</Name>
			<Position>Salesperson</Position>
			<Phone>555-4580 ext. 105</Phone>
			<Years>5</Years>
		</Employee>
		<Employee Status="Full-time">
			<Name>Richard Brown</Name>
			<Position>Shop Manager</Position>
			<Phone>555-4580 ext. 193</Phone>
			<Years>15</Years>
		</Employee>
		<Employee Status="Full-time">
			<Name>John Jacobs</Name>
			<Position>Plant Coordinator</Position>
			<Phone>555-4580 ext. 203</Phone>
			<Years>13</Years>
		</Employee>
		<Employee Status="Full-time">
			<Name>Linda Laurence</Name>
			<Position>Manufacturing Executive</Position>
			<Phone>555-4580 ext. 472</Phone>
			<Years>12</Years>
		</Employee>
		<Employee Status="Full-time">
			<Name>Ian Tompkins</Name>
			<Position>Shop Steward</Position>
			<Phone>555-4580 ext. 539</Phone>
			<Years>9</Years>
		</Employee>
		<Employee Status="Full-time">
			<Name>Maureen Charnas</Name>
			<Position>Advertising Executive</Position>
			<Phone>555-4580 ext. 804</Phone>
			<Years>1</Years>
		</Employee>
		<Employee Status="Full-time">
			<Name>Robert Kuhlman</Name>
			<Position>Advertising Executive</Position>
			<Phone>555-4580 ext. 941</Phone>
			<Years>10</Years>
		</Employee>
		<Employee Status="Full-time">
			<Name>Steve Lawson</Name>
			<Position>Media Officer</Position>
			<Phone>555-4580 ext. 111</Phone>
			<Years>1</Years>
		</Employee>
		<Employee Status="Full-time">
			<Name>Paul Unwin</Name>
			<Position>Media Manager</Position>
			<Phone>555-4580 ext. 150</Phone>
			<Years>3</Years>
		</Employee>
</Staff>

Open in new window

0
 
catonthecouchproductionsAuthor Commented:
Hey!

It is working now, it outputs with out the deleted record. But it doesnt seem to save it. If i open the XML file its still there, but the output is correct.
<?php 
error_reporting(E_ALL);
include ("config.inc.php");
 if(isset($_GET['delete'])){
	$query = 'DELETE FROM tblemp WHERE name LIKE "'.$_GET['delete'].'%"';
	mysql_query($query) or die(mysql_error());
}
//header('Location: http://localhost:8888/xml_final/deleted.html');
?>

<?php

$doc = new DOMDocument; 
$doc->load('All_emp2.xml');

$thedocument = $doc->documentElement;

//this gives you a list of the messages
$list = $thedocument->getElementsByTagName('Employee');

//figure out which ones you want -- assign it to a variable (ie: $nodeToRemove )
$nodeToRemove = null;
     foreach ($list as $domElement){
  $attrValue = $domElement->getElementsByTagName('Name');
  if ($attrValue->length && 
       $attrValue->item(0)->textContent == $_GET['delete']) {
    $nodeToRemove = $domElement; 
  }
}



//Now remove it.
if ($nodeToRemove != null)
$thedocument->removeChild($nodeToRemove);

echo $doc->saveXML(); 
?>

Open in new window

0
 
TiberiuGalCommented:
saveXML does not save the file, but outputs the content as XML.
you should use save(filename);
http://www.php.net/manual/en/domdocument.save.php
0
 
catonthecouchproductionsAuthor Commented:
Perfect...thank you.
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.