Bazi
Bazi

Reputation: 195

Remove certain elements from XML with PHP

Originally I want to split an XML-File into two files. Therefore I have to remove some elements.

XML looks like this:

<?xml version="1.0"?>
<events>
  <event>
    <id>1</id>
    <typ>new</typ>
  </event>
  <event>
    <id>2</id>
    <typ>new</typ>
  </event>
  <event>
    <id>3</id>
    <typ>old</typ>
  </event>
  <event>
    <id>4</id>
    <typ>new</typ>
  </event>
  <event>
    <id>5</id>
    <typ>old</typ>
  </event>
  <event>
    <id>6</id>
    <typ>old</typ>
  </event>
  <event>
    <id>7</id>
    <typ>old</typ>
  </event>
</events>

What I need is:

<?xml version="1.0"?>
<events>
  <event>
    <id>1</id>
    <typ>new</typ>
  </event>
  <event>
    <id>2</id>
    <typ>new</typ>
  </event>
  <event>
    <id>4</id>
    <typ>new</typ>
  </event>
</events>

So I have to remove all events with typ = old. I've tried it like this:

$events = file_get_contents("events.xml");
$doc = new SimpleXMLElement($events);
foreach($doc->event as $seg)
{
  if ($seg->typ == 'new') {
    echo "$seg->typ<br />";
    unset($doc->event[$seg]);
  }
}
echo $doc->asXml();

But only the echo is right, the rest is still the same. Then I found this function:

function delete_event_typ($typ, $filename = 'events.xml'){
  $data = simplexml_load_file($filename);
  for($i = 0, $length = count($data->event); $i < $length; $i++){
    if($data->event[$i]->typ == $typ){
      unset($data->event[$i]);
    }
  }
  file_put_contents($filename, $data->saveXML());
}
//SAMPLE USAGE
delete_event_typ('old');  

But it deletes not all entries, it looks like:

<?xml version="1.0"?>
<events>
  <event>
    <id>1</id>
    <typ>new</typ>
  </event>
  <event>
    <id>2</id>
    <typ>new</typ>
  </event>

  <event>
    <id>4</id>
    <typ>new</typ>
  </event>

  <event>
    <id>6</id>
    <typ>old</typ>
  </event>

</events>

So what else can I do? Any ideas?

Upvotes: 1

Views: 77

Answers (1)

splash58
splash58

Reputation: 26143

Make loop in reverse order

for($c = count($doc->event); $c >= 0 ; $c--)
{
  if ($doc->event[$c]->typ == 'old') {
    unset($doc->event[$c]);
  }
}

Upvotes: 1

Related Questions