Read DOM Document then Delete the node

I am using PHP.
I am reading an XML document using DOMDocument, I run through a foreach to loop through nodes.  After I read an element node, pull out all of its children... I then need to remove that node from the xml file.

Any one have examples as to how to accomplish this.  I seem to read it in ok.  I am having a hard time deleting the node i just read, then saving the xml file, wash... rinse... repeat through the entire xml file.

Any help is appreciated.

Thanks
jrlitmAsked:
Who is Participating?
 
Roger BaklundConnect With a Mentor Commented:
There are some problems related to using foreach() with a DOMNodeList object. This is a "live" sequence object, deleting an item at the start changes the index for all following items. Looping backwards eliminates this problem.
   function Xml_Dataclear($xmlfile) {
     $dom=new DOMDocument;
     $dom->formatOutput = true;
     $dom->preserveWhiteSpace = false;
     $dom->validateOnParse = true;
     $dom->load($xmlfile);
     $issues = $dom->getElementsByTagName("issue");
     for($i=$issues->length-1;$i>=0;$i--) {
       $issue = $issues->item($i);
       while($issue->hasChildNodes())
         $issue->removeChild($issue->childNodes->item(0));
       $issue->parentNode->removeChild($issue);  
     }
     $dom->save($xmlfile) or die("Error saving to file");
   }

Open in new window

0
 
Roger BaklundCommented:
You should use remove_child(), there is an example here:

http://php.net/manual/en/function.domnode-remove-child.php
0
 
jrlitmAuthor Commented:
I see the example.  However I am not quite getting it to fit into place.

In the attached example I have the data I need.  Now I need to clear this out or delete this node before I go onto the next node.

Ideas?

Thanks

$xmlfile =  'external/external_post.xml';
$doc = new DOMDocument();
$doc->load( $xmlfile );
 
$issues = $doc->getElementsByTagName( "issue" );
foreach( $issues as $issue )
{
 
$issue_titles= $issue->getElementsByTagName( "issue_title" );
$issue_title= $issue_titles->item(0)->nodeValue;
 
$issues = $issue->getElementsByTagName( "issue" );
$issue = $issues->item(0)->nodeValue;
 
}

Open in new window

0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Roger BaklundCommented:
First, there is something strange in the code you posted. You create a list in line 5, and loop through it, starting at line 6. This loop includes lines 12 and 13, where you re-assign $issues, the variable you are looping trough, and $issue, the variable php is re-creating in line 6 for each iteration of the loop... just remove those two lines (12 & 13).

You can not update an xml file directly, as if it was a database. It is possible to update an xml file, but it is not practical. It is much easier to create a new file with the needed modification, in this case without the nodes you do not want.
0
 
jrlitmAuthor Commented:
Hi CXR,

Ok I am close to figuring this out:

xml File:
<issues>
  <issue acc_num="23452345">
    <issue_title>Title</issue_title>
    <issue_desc>Descriptions</issue_desc>
  </issue>
 <issue acc_num="23433345">
    <issue_title>Title</issue_title>
    <issue_desc>Descriptions</issue_desc>
  </issue>
 <issue acc_num="23333345">
    <issue_title>Title</issue_title>
    <issue_desc>Descriptions</issue_desc>
  </issue>
</issues>


The code snippet seems to go through and remove all the children under <issues> except for the last one.  It always leaves one behind.

Any ideas as to why it is not finishing.

Thanks
   function Xml_Dataclear(){
        $xmlfile =  'external/external_post.xml';   
        $dom=new DOMDocument;
        $dom->formatOutput = true;
        $dom->preserveWhiteSpace = false;
        $dom->validateOnParse = true;
        $dom->load($xmlfile);
        //$dom->loadXML($xmlfile);
 
       foreach($issues as $key => $issue){
            while($issue->hasChildNodes()){
                $issue->removeChild($issue->childNodes->item(0));
            }
            $issue->parentNode->removeChild($issue);  
        } 
        while($issue->hasChildNodes()){
        $issue->removeChild($issue->childNodes->item(0));
        }
        $issue->parentNode->removeChild($issue);      
       $dom->save($xmlfile) or die ("Error saving to file");
       
   }

Open in new window

0
 
jrlitmAuthor Commented:
Ok just to recap for anyone that is interested.  

With Cxr's gentle proding... I have fixed this.  I had to resolve this issue with two read processes.  I am sure there is an easier way... but this seemed to work for me.

This app runs in batch mode.  A data form updates an xml file, and in regular intervals a cron event runs this script to post the data into the database.

The script:
I read the data in.  Processed the data.  Verified it processed correctly, then remove the data at the end.

For some reason reading an element, processing then removing that element before reading the next element caused me sleepless nights.  I think Cxr was trying to tell me that it would cause me difficulty, but I was too stubborn to let it sink in.

The last post from Cxr finished the script.  It worked perfectly and is a very important issue with DOMNodeList.  

This is complete.  Thank you Cxr for your help!!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.