I have an xml file like this which have some alerts alert.xml
<?xml version="1.0"?>
<alerts>
<alert>
<id>3</id>
<msg_type>1</msg_type>
<msg>Some Message</msg>
<url>Some Link</url>
<status>0</status>
<create_date>1388049941</create_date>
<update_date>1388052529</update_date>
</alert>
<alert>
<id>6</id>
<msg_type>1</msg_type>
<msg>Some Message</msg>
<url>Some Link</url>
<status>0</status>
<create_date>1388049941</create_date>
<update_date>1388052529</update_date>
</alert>
<alert>
<id>14</id>
<msg_type>1</msg_type>
<msg>Some Message</msg>
<url>Some Link</url>
<status>0</status>
<create_date>1388049941</create_date>
<update_date>1388052529</update_date>
</alert>
<alert>
<id>24</id>
<msg_type>1</msg_type>
<msg>Some Message</msg>
<url>Some Link</url>
<status>0</status>
<create_date>1388049941</create_date>
<update_date>1388052529</update_date>
</alert>
</alerts>
now I am reading this file using php like this to find a record.
$alerts = new simpleXMLElement($filename, null, true);
$search_mediums1 = $alerts->xpath('alert[msg_type=1]');
$search_mediums = array_reverse($search_mediums1);
foreach ($search_mediums as $arr_alert) {
if ($arr_alert->id == $my_id) {
// i want some code here to delete that node
// and put it into some other xml with same structure file
break;
}
}
I have find some solutions with DOMDocument(); class but I am using here simpleXMLElement
Its working fine to find the record of desired msg_type=1 and id=$my_id now I want to remove the node from alert.xml file, if find and put it into other xml file which have same format as alert.xml. How can I remove the node from 'alert.xml' and put it in another xml file ?
removeChild()method or alike in SimpleXMLElement. While it may still be doable, I would recommend to useDOMDocumentinstead.DOMDocumenthas XPath support too: php.net/manual/en/class.domxpath.php