Page 1 of 1
remove elements from XML with php
Posted: Mon Jul 18, 2005 7:53 am
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'm naughty, are you naughty?'>smurf</span> problem, please someone can help me?
Posted: Mon Jul 18, 2005 8:21 am
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;
}
I'm not sure if you understand my question
Posted: Mon Jul 18, 2005 3:43 pm
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

Posted: Mon Jul 18, 2005 4:06 pm
by Chris Corbyn
So didn't my function work?

What did it do? I might just need to tweak it a little

Posted: Mon Jul 18, 2005 4:10 pm
by Chris Corbyn
Bah sorry I'm such a n00b

It's missing the $string parameter (which whould alose be the third parameter in preg_replace()... It needs tweaking though... gimme 5 mins

Posted: Mon Jul 18, 2005 4:12 pm
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>
*/
?>
Thanks but now ?
Posted: Tue Jul 19, 2005 10:17 am
by teketen
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.
