remove elements from XML with php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
teketen
Forum Newbie
Posts: 4
Joined: Mon Jul 18, 2005 7:45 am

remove elements from XML with php

Post by teketen »

I have a xml well formed file (automatiko.xml)
with this structure:
<Automatiko>
<Agenda>
<Izena>Proba1</Izena>
<Helbidea>http://sample_url</Helbidea>
</Agenda>
<Agenda>
<Izena>Proba2</Izena>
<Helbidea>http://sample_url2</Helbidea>
</Agenda>
</Automatiko>

I want to do a function giving a url of the element I want to remove, remove the element.

function xml_remove($url_to_remove)

example: if y put xml_remove('http://sample_url') -->

the result:
<Automatiko>
<Agenda>
<Izena>Proba2</Izena>
<Helbidea>http://sample_url2</Helbidea>
</Agenda>
</Automatiko>

I try a lot of things but I can solve this <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span> problem, please someone can help me?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Untested....

There are some XML functions to if you have libxml compiled....

Code: Select all

function xml_remove($url) {

    $re = '@^<agenda>$\\s*^<izena>.*?</izena>$\\s*^<helbidea>'.addslashes($url).'</helbidea>$\\s*^</agenda>$\\s*@im';
    $url = preg_replace($re, '', $url);
    return $url;

}
teketen
Forum Newbie
Posts: 4
Joined: Mon Jul 18, 2005 7:45 am

I'm not sure if you understand my question

Post by teketen »

the function have to remove all Agenda element containing the given $url reading xml from file.

read from file automatiko.xml:
<Automatiko>
<Agenda>
<Izena>Proba1</Izena>
<Helbidea>http://sample_url</Helbidea>
</Agenda>
<Agenda>
<Izena>Proba2</Izena>
<Helbidea>http://sample_url2</Helbidea>
</Agenda>
</Automatiko>

the result of the function in automatiko.xml after the action of the function xml_remove('http://sample_url') will be:
<Automatiko>
<Agenda>
<Izena>Proba2</Izena>
<Helbidea>http://sample_url2</Helbidea>
</Agenda>
</Automatiko>

this part is removed from the automatiko.xml file:
<Agenda>
<Izena>Proba1</Izena>
<Helbidea>http://sample_url</Helbidea>
</Agenda
>

thanks for all :oops: :?:
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

So didn't my function work? :? What did it do? I might just need to tweak it a little ;)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Bah sorry I'm such a n00b :P It's missing the $string parameter (which whould alose be the third parameter in preg_replace()... It needs tweaking though... gimme 5 mins ;)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Code: Select all

<?php

$string = "
<Automatiko>
<Agenda>
<Izena>Proba1</Izena>
<Helbidea>http://sample_url</Helbidea>
</Agenda>
<Agenda>
<Izena>Proba2</Izena>
<Helbidea>http://sample_url2</Helbidea>
</Agenda>
</Automatiko>
";

function xml_remove($url, $xml) {
 
    $re = '@^<agenda>$\\s*^<izena>.*?</izena>$\\s*^<helbidea>'.addslashes($url).'</helbidea>$\\s*^</agenda>$\\s*@im';
    $xml = preg_replace($re, '', $xml);
    return $xml;
 
}

$new_string  = xml_remove('http://sample_url', $string);
echo $new_string;

/*
<Automatiko>
<Agenda>
<Izena>Proba2</Izena>
<Helbidea>http://sample_url2</Helbidea>
</Agenda>
</Automatiko>
*/
?>
teketen
Forum Newbie
Posts: 4
Joined: Mon Jul 18, 2005 7:45 am

Thanks but now ?

Post by teketen »

Thanks for all, :P

now I have to do a script that automates the activity of a script(which read from automatiko.xml). I think I'm going to have problems with the automatic functions. :oops:
Post Reply