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'm naughty, are you naughty?'>smurf</span> problem, please someone can help me?
remove elements from XML with php
Moderator: General Moderators
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Untested....
There are some XML functions to if you have libxml compiled....
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;
}I'm not sure if you understand my question
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:
thanks for all

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
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
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>
*/
?>Thanks but now ?
Thanks for all, 
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.
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.