57

I have several identical elements with different attributes that I'm accessing with SimpleXML:

<data>
    <seg id="A1"/>
    <seg id="A5"/>
    <seg id="A12"/>
    <seg id="A29"/>
    <seg id="A30"/>
</data>

I need to remove a specific seg element, with an id of "A12", how can I do this? I've tried looping through the seg elements and unsetting the specific one, but this doesn't work, the elements remain.

foreach($doc->seg as $seg)
{
    if($seg['id'] == 'A12')
    {
        unset($seg);
    }
}
Stefan Gehrig
  • 82,642
  • 24
  • 155
  • 189
TimTowdi
  • 647
  • 1
  • 6
  • 7

18 Answers18

60

Contrary to popular belief in the existing answers, each Simplexml element node can be removed from the document just by itself and unset(). The point in case is just that you need to understand how SimpleXML actually works.

First locate the element you want to remove:

list($element) = $doc->xpath('/*/seg[@id="A12"]');

Then remove the element represented in $element you unset its self-reference:

unset($element[0]);

This works because the first element of any element is the element itself in Simplexml (self-reference). This has to do with its magic nature, numeric indices are representing the elements in any list (e.g. parent->children), and even the single child is such a list.

Non-numeric string indices represent attributes (in array-access) or child-element(s) (in property-access).

Therefore numeric indecies in property-access like:

unset($element->{0});

work as well.

Naturally with that xpath example, it is rather straight forward (in PHP 5.4):

unset($doc->xpath('/*/seg[@id="A12"]')[0][0]);

The full example code (Demo):

<?php
/**
 * Remove a child with a specific attribute, in SimpleXML for PHP
 * @link http://stackoverflow.com/a/16062633/367456
 */

$data=<<<DATA
<data>
    <seg id="A1"/>
    <seg id="A5"/>
    <seg id="A12"/>
    <seg id="A29"/>
    <seg id="A30"/>
</data>
DATA;


$doc = new SimpleXMLElement($data);

unset($doc->xpath('seg[@id="A12"]')[0]->{0});

$doc->asXml('php://output');

Output:

<?xml version="1.0"?>
<data>
    <seg id="A1"/>
    <seg id="A5"/>

    <seg id="A29"/>
    <seg id="A30"/>
</data>
hakre
  • 193,403
  • 52
  • 435
  • 836
  • This self-reference technique has been earlier (Nov 2010) demonstrated in: [an answer to *"PHP SimpleXML - Remove xpath node"*](http://stackoverflow.com/a/4137027/367456). – hakre May 21 '13 at 14:23
  • 1
    And this simplexml self-reference technique has been earlier (Jun 2010) demonstrated in: [an answer to *"How can I set text value of SimpleXmlElement without using its parent?"*](http://stackoverflow.com/a/3153704/367456) – hakre Jun 22 '13 at 08:20
  • 6
    Very well explained answer. One detail I didn't immediately appreciate is that you can't trivially take XPath out of the loop, because deleting an element inside a normal `foreach ( $doc->seg as $seg )` loop confuses the iterator (rule of thumb: don't change the length of an iterator mid-loop). SimpleXML's XPath implementation doesn't have this problem because its results are an ordinary array of unrelated elements. – IMSoP Sep 12 '13 at 20:06
  • 2
    @IMSoP: For any `Traversable` and that issue (*live lists*), I can highly recommend [`iterator_to_array`](http://php.net/iterator_to_array), in SimpleXML iterators set the key parameter to FALSE because SimpleXMLElement uses the tag-name as key which often is duplicate in such a listing and then that function would return only the last of these same-named nodes if the second parameter is not `FALSE`. – hakre Sep 30 '13 at 09:00
  • 2
    Good tip, particularly regarding the extra parameter. :) – IMSoP Sep 30 '13 at 09:21
60

While SimpleXML provides a way to remove XML nodes, its modification capabilities are somewhat limited. One other solution is to resort to using the DOM extension. dom_import_simplexml() will help you with converting your SimpleXMLElement into a DOMElement.

Just some example code (tested with PHP 5.2.5):

$data='<data>
    <seg id="A1"/>
    <seg id="A5"/>
    <seg id="A12"/>
    <seg id="A29"/>
    <seg id="A30"/>
</data>';
$doc=new SimpleXMLElement($data);
foreach($doc->seg as $seg)
{
    if($seg['id'] == 'A12') {
        $dom=dom_import_simplexml($seg);
        $dom->parentNode->removeChild($dom);
    }
}
echo $doc->asXml();

outputs

<?xml version="1.0"?>
<data><seg id="A1"/><seg id="A5"/><seg id="A29"/><seg id="A30"/></data>

By the way: selecting specific nodes is much more simple when you use XPath (SimpleXMLElement->xpath):

$segs=$doc->xpath('//seq[@id="A12"]');
if (count($segs)>=1) {
    $seg=$segs[0];
}
// same deletion procedure as above
Community
  • 1
  • 1
Stefan Gehrig
  • 82,642
  • 24
  • 155
  • 189
  • 5
    Thanks for this - initially I was inclined to avoid this answer since I wanted to avoid using DOM. I tried several other answers which did not work, before finally trying yours - which worked flawlessly. To anyone considering avoiding this answer, try it out first and see if it doesn't do exactly what you want. I think what threw me off was I didn't realize dom_import_simplexml() still works with the same underlying structure as the simplexml, so any changes in one immediately effect the other, no need to write/read or reload. – dimo414 Jul 05 '10 at 02:01
  • 11
    Note that this code will only remove the first element encountered. I suspect that this is because modifying the data while it's under iteration invalidates the iterator position, thus causing the foreach loop to terminate. I solved this by saving the dom-imported nodes to an array which I then iterated through to perform the deletion. Not a great solution, but it works. – Ryan Ballantyne Sep 12 '10 at 03:50
  • 4
    You can actually delete SimpleXML elements using unset, see posthy's answer for a solution. – François Feugeas Aug 17 '11 at 16:05
  • 2
    Actually you can delete SimpleXML elements using unset, but it's in my answer ;) http://stackoverflow.com/a/16062633/367456 – hakre Apr 17 '13 at 14:27
  • Unset wasn't working for me, but the dom method worked extremely well. Thanks for that! – Jarvis Jul 20 '16 at 17:15
26

Just unset the node:

$str = <<<STR
<a>
  <b>
    <c>
    </c>
  </b>
</a>
STR;

$xml = simplexml_load_string($str);
unset($xml –> a –> b –> c); // this would remove node c
echo $xml –> asXML(); // xml document string without node c

This code was taken from How to delete / remove nodes in SimpleXML.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
datasn.io
  • 12,564
  • 28
  • 113
  • 154
  • 6
    This only works if the node name is unique in the set. If it's not, you end up removing all nodes of the same name. – Dallas Dec 06 '12 at 16:34
  • 3
    @Dallas: What you comment is right, but it also contains the solution. How to access the first element only? See here: http://stackoverflow.com/a/16062633/367456 – hakre Apr 17 '13 at 14:30
11

I believe Stefan's answer is right on. If you want to remove only one node (rather than all matching nodes), here is another example:

//Load XML from file (or it could come from a POST, etc.)
$xml = simplexml_load_file('fileName.xml');

//Use XPath to find target node for removal
$target = $xml->xpath("//seg[@id=$uniqueIdToDelete]");

//If target does not exist (already deleted by someone/thing else), halt
if(!$target)
return; //Returns null

//Import simpleXml reference into Dom & do removal (removal occurs in simpleXML object)
$domRef = dom_import_simplexml($target[0]); //Select position 0 in XPath array
$domRef->parentNode->removeChild($domRef);

//Format XML to save indented tree rather than one line and save
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($xml->asXML());
$dom->save('fileName.xml');

Note that sections Load XML... (first) and Format XML... (last) could be replaced with different code depending on where your XML data comes from and what you want to do with the output; it is the sections in between that find a node and remove it.

In addition, the if statement is only there to ensure that the target node exists before trying to move it. You could choose different ways to handle or ignore this case.

Witman
  • 1,488
  • 2
  • 15
  • 19
  • 1
    Note that xpath() returns an empty array if nothing was found, so the check $target == false should be empty($target). +1 for xpath solution – Markus Hedlund Jan 13 '10 at 12:01
6

This work for me:

$data = '<data>
<seg id="A1"/>
<seg id="A5"/>
<seg id="A12"/>
<seg id="A29"/>
<seg id="A30"/></data>';

$doc = new SimpleXMLElement($data);

$segarr = $doc->seg;

$count = count($segarr);

$j = 0;

for ($i = 0; $i < $count; $i++) {

    if ($segarr[$j]['id'] == 'A12') {
        unset($segarr[$j]);
        $j = $j - 1;
    }
    $j = $j + 1;
}

echo $doc->asXml();
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
sunnyface45
  • 61
  • 1
  • 1
4

If you extend the base SimpleXMLElement class, you can use this method:

class MyXML extends SimpleXMLElement {

    public function find($xpath) {
        $tmp = $this->xpath($xpath);
        return isset($tmp[0])? $tmp[0]: null;
    }

    public function remove() {
        $dom = dom_import_simplexml($this);
        return $dom->parentNode->removeChild($dom);
    }

}

// Example: removing the <bar> element with id = 1
$foo = new MyXML('<foo><bar id="1"/><bar id="2"/></foo>');
$foo->find('//bar[@id="1"]')->remove();
print $foo->asXML(); // <foo><bar id="2"/></foo>
Michał Tatarynowicz
  • 1,294
  • 2
  • 15
  • 33
2

For future reference, deleting nodes with SimpleXML can be a pain sometimes, especially if you don't know the exact structure of the document. That's why I have written SimpleDOM, a class that extends SimpleXMLElement to add a few convenience methods.

For instance, deleteNodes() will delete all nodes matching a XPath expression. And if you want to delete all nodes with the attribute "id" equal to "A5", all you have to do is:

// don't forget to include SimpleDOM.php
include 'SimpleDOM.php';

// use simpledom_load_string() instead of simplexml_load_string()
$data = simpledom_load_string(
    '<data>
        <seg id="A1"/>
        <seg id="A5"/>
        <seg id="A12"/>
        <seg id="A29"/>
        <seg id="A30"/>
    </data>'
);

// and there the magic happens
$data->deleteNodes('//seg[@id="A5"]');
Josh Davis
  • 28,400
  • 5
  • 52
  • 67
2

To remove/keep nodes with certain attribute value or falling into array of attribute values you can extend SimpleXMLElement class like this (most recent version in my GitHub Gist):

class SimpleXMLElementExtended extends SimpleXMLElement
{    
    /**
    * Removes or keeps nodes with given attributes
    *
    * @param string $attributeName
    * @param array $attributeValues
    * @param bool $keep TRUE keeps nodes and removes the rest, FALSE removes nodes and keeps the rest 
    * @return integer Number o affected nodes
    *
    * @example: $xml->o->filterAttribute('id', $products_ids); // Keeps only nodes with id attr in $products_ids
    * @see: http://stackoverflow.com/questions/17185959/simplexml-remove-nodes
    */
    public function filterAttribute($attributeName = '', $attributeValues = array(), $keepNodes = TRUE)
    {       
        $nodesToRemove = array();

        foreach($this as $node)
        {
            $attributeValue = (string)$node[$attributeName];

            if ($keepNodes)
            {
                if (!in_array($attributeValue, $attributeValues)) $nodesToRemove[] = $node;
            }
            else
            { 
                if (in_array($attributeValue, $attributeValues)) $nodesToRemove[] = $node;
            }
        }

        $result = count($nodesToRemove);

        foreach ($nodesToRemove as $node) {
            unset($node[0]);
        }

        return $result;
    }
}

Then having your $doc XML you can remove your <seg id="A12"/> node calling:

$data='<data>
    <seg id="A1"/>
    <seg id="A5"/>
    <seg id="A12"/>
    <seg id="A29"/>
    <seg id="A30"/>
</data>';

$doc=new SimpleXMLElementExtended($data);
$doc->seg->filterAttribute('id', ['A12'], FALSE);

or remove multiple <seg /> nodes:

$doc->seg->filterAttribute('id', ['A1', 'A12', 'A29'], FALSE);

For keeping only <seg id="A5"/> and <seg id="A30"/> nodes and removing the rest:

$doc->seg->filterAttribute('id', ['A5', 'A30'], TRUE);
Krzysztof Przygoda
  • 1,217
  • 1
  • 17
  • 24
1

Even though SimpleXML doesn't have a detailed way to remove elements, you can remove elements from SimpleXML by using PHP's unset(). The key to doing this is managing to target the desired element. At least one way to do the targeting is using the order of the elements. First find out the order number of the element you want to remove (for example with a loop), then remove the element:

$target = false;
$i = 0;
foreach ($xml->seg as $s) {
  if ($s['id']=='A12') { $target = $i; break; }
  $i++;
}
if ($target !== false) {
  unset($xml->seg[$target]);
}

You can even remove multiple elements with this, by storing the order number of target items in an array. Just remember to do the removal in a reverse order (array_reverse($targets)), because removing an item naturally reduces the order number of the items that come after it.

Admittedly, it's a bit of a hackaround, but it seems to work fine.

Ilari Kajaste
  • 3,207
  • 2
  • 23
  • 25
  • You can also use the self-reference which allows to unset any element without knowing it's offset. [A single variable is enough](http://stackoverflow.com/a/16062633/367456). – hakre Apr 17 '13 at 14:33
1

A new idea: simple_xml works as a array.

We can search for the indexes of the "array" we want to delete, and then, use the unset() function to delete this array indexes. My example:

$pos=$this->xml->getXMLUser();
$i=0; $array_pos=array();
foreach($this->xml->doc->users->usr[$pos]->u_cfg_root->profiles->profile as $profile) {
    if($profile->p_timestamp=='0') { $array_pos[]=$i; }
    $i++;
}
//print_r($array_pos);
for($i=0;$i<count($array_pos);$i++) {
    unset($this->xml->doc->users->usr[$pos]->u_cfg_root->profiles->profile[$array_pos[$i]]);
}
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
joan16v
  • 11
  • 1
1

There is a way to remove a child element via SimpleXml. The code looks for a element, and does nothing. Otherwise it adds the element to a string. It then writes out the string to a file. Also note that the code saves a backup before overwriting the original file.

$username = $_GET['delete_account'];
echo "DELETING: ".$username;
$xml = simplexml_load_file("users.xml");

$str = "<?xml version=\"1.0\"?>
<users>";
foreach($xml->children() as $child){
  if($child->getName() == "user") {
      if($username == $child['name']) {
        continue;
    } else {
        $str = $str.$child->asXML();
    }
  }
}
$str = $str."
</users>";
echo $str;

$xml->asXML("users_backup.xml");
$myFile = "users.xml";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $str);
fclose($fh);
0

Idea about helper functions is from one of the comments for DOM on php.net and idea about using unset is from kavoir.com. For me this solution finally worked:

function Myunset($node)
{
 unsetChildren($node);
 $parent = $node->parentNode;
 unset($node);
}

function unsetChildren($node)
{
 while (isset($node->firstChild))
 {
 unsetChildren($node->firstChild);
 unset($node->firstChild);
 }
}

using it: $xml is SimpleXmlElement

Myunset($xml->channel->item[$i]);

The result is stored in $xml, so don’t worry about assigning it to any variable.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
Urszula Karzelek
  • 556
  • 6
  • 15
0

I was also strugling with this issue and the answer is way easier than those provided over here. you can just look for it using xpath and unset it it the following method:

unset($XML->xpath("NODESNAME[@id='test']")[0]->{0});

this code will look for a node named "NODESNAME" with the id attribute "test" and remove the first occurence.

remember to save the xml using $XML->saveXML(...);

Ben Yitzhaki
  • 1,376
  • 16
  • 31
0

Since I encountered the same fatal error as Gerry and I'm not familiar with DOM, I decided to do it like this:

$item = $xml->xpath("//seg[@id='A12']");
$page = $xml->xpath("/data");
$id = "A12";

if (  count($item)  &&  count($page) ) {
    $item = $item[0];
    $page = $page[0];

     // find the numerical index within ->children().
    $ch = $page->children();
    $ch_as_array = (array) $ch;

    if (  count($ch_as_array)  &&  isset($ch_as_array['seg'])  ) {
        $ch_as_array = $ch_as_array['seg'];
        $index_in_array = array_search($item, $ch_as_array);
        if (  ($index_in_array !== false)
          &&  ($index_in_array !== null)
          &&  isset($ch[$index_in_array])
          &&  ($ch[$index_in_array]['id'] == $id)  ) {

             // delete it!
            unset($ch[$index_in_array]);

            echo "<pre>"; var_dump($xml); echo "</pre>";
        }
    }  // end of ( if xml object successfully converted to array )
}  // end of ( valid item  AND  section )
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
WoodrowShigeru
  • 1,418
  • 1
  • 18
  • 25
0

With FluidXML you can use XPath to select the elements to remove.

$doc = fluidify($doc);

$doc->remove('//*[@id="A12"]');

https://github.com/servo-php/fluidxml


The XPath //*[@id="A12"] means:

  • in any point of the document (//)
  • every node (*)
  • with the attribute id equal to A12 ([@id="A12"]).
Daniele Orlando
  • 2,692
  • 3
  • 23
  • 26
0

If you want to cut list of similar (not unique) child elements, for example items of RSS feed, you could use this code:

for ( $i = 9999; $i > 10; $i--) {
    unset($xml->xpath('/rss/channel/item['. $i .']')[0]->{0});
}

It will cut tail of RSS to 10 elements. I tried to remove with

for ( $i = 10; $i < 9999; $i ++ ) {
    unset($xml->xpath('/rss/channel/item[' . $i . ']')[0]->{0});
}

But it works somehow randomly and cuts only some of the elements.

Columbus
  • 1
  • 1
0

I had a similar task - remove child elements, that are already present with the specified attribute. In other words, remove duplicates in xml. I have the following xml structure:

<rups>
    <rup id="1">
         <profiles> ... </profiles>
         <sections>
             <section id="1.1" num="Б1.В" parent_id=""/>
             <section id="1.1.1" num="Б1.В.1" parent_id="1.1"/>
             ...
             <section id="1.1" num="Б1.В" parent_id=""/>
             <section id="1.1.2" num="Б1.В.2" parent_id="1.1"/>
             ...
         </sections>
    </rup>
    <rup id="2">
         ...
    </rup>
    ...
 </rups>

For example, rups/rup[@id='1']/sections/section[@id='1.1'] elements are duplicated and I only need to leave the first one. I'm using a reference to array of elements, loop-for and unset():

$xml = simplexml_load_file('rup.xml');
foreach ($xml->rup as $rup) {
    $r_s = [];
    $bads_r_s = 0;
    $sections = &$rup->sections->section;
    for ($i = count($sections)-1; $i >= 0; --$i) {
        if (in_array((string)$sections[$i]['id'], $r_s)) {
            $bads_r_s++;
            unset($sections[$i]);
            continue;
        }
        $r_s[] = (string)$sections[$i]['id'];
    }
}
$xml->saveXML('rup_checked.xml');
Dharman
  • 30,962
  • 25
  • 85
  • 135
-2

Your initial approach was right, but you forgot one little thing about foreach. It doesn't work on the original array/object, but creates a copy of each element as it iterates, so you did unset the copy. Use reference like this:

foreach($doc->seg as &$seg) 
{
    if($seg['id'] == 'A12')
    {
        unset($seg);
    }
}
posthy
  • 69
  • 1
  • 1
  • This answer needs way more love as everyone is coming up with very complicated solutions to a very simple mistake ! – François Feugeas Aug 17 '11 at 16:04
  • 15
    "Fatal error: An iterator cannot be used with foreach by reference" – Gerry Mar 20 '13 at 07:47
  • For those wondering about an iterator error, see [comment here](http://stackoverflow.com/questions/262351/remove-a-child-with-a-specific-attribute-in-simplexml-for-php?rq=1#comment27677561_16062633) – Robert Dundon Apr 25 '17 at 19:45