0

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 ?

4

2 Answers 2

2

According to PHP Doc, SimpleXMLElement doesn't have a removeChild() method or alike, so you may have better luck with DOMDocument instead.

Changing this code to DOM is straightforward, although DOM codes will seem a little long and verbose...

$dom=new DOMDocument();
$dom->loadXML(/* XML content */);
$xpath=new DOMXPath($dom);
$nodeList=$xpath->query('alert[msg_type=1]');
$myID=14; // Just to demonstrate
foreach($nodeList as $node)
{
    foreach($node->childNodes as $child)
    {
        if($child->nodeName=="id")
        {
            if($child->textContent==$myID)
            {
                $node->parentNode->removeChild($node);
            }
            break;
        }
    }
}
echo $dom->saveXML();

Online demo

The output:

<?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>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>

You can see the <alert> with <id>14</id> is removed.


Additional:

To move the deleted node to another document, do this:

$target=new DOMDocument();
$target->loadXML(/* xml */);
/* ... */
if($child->textContent==$myID)
{
    $target->documentElement->appendChild($target->importNode($node,true));
    $node->parentNode->removeChild($node);
}
/* ... */

Online demo

Sign up to request clarification or add additional context in comments.

2 Comments

thanks bro !! i tried and use the dom_import_simplexml method and its worked now how can i put that node into another xml file
thanks!! i tried your code in my own answer and its working. i am updating my own answer for issue-2. please see it. i am really thankful to for your help. thanks bro !!
1

i have solved one issue that remove the desired node using dom_import_simplexml method as follows

$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)
    {
       // Deleting the Node 
        $domRef = dom_import_simplexml($arr_alert);
        $domRef->parentNode->removeChild($domRef);
        $dom = new DOMDocument('1.0');
        $dom->preserveWhiteSpace = false;
        $dom->formatOutput = true;
        $dom->loadXML($alerts->asXML());
        $dom->save($filename);


       // moving the Node into another file
        $target=new DOMDocument();
        $target->formatOutput = true;
        $target->preserveWhiteSpace = false;
        $target_content = file_get_contents($target_filename);
        $target->loadXML($target_content);
        $target->documentElement->appendChild($target->importNode($domRef,true));
        $target->save($target_filename);
        break;
    }
}

1 Comment

i find the anwer in my code with help of @Passerby. thanks a lot Passerby

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.