0

Here is where I set up basic variables, such as creating the new DomDoc and such as well as loading some of the Tags. This all works fine at the moment.

  <?php 
if (isset($_GET['edit'])&& $_GET['edit']=='delete' && isset($_GET['id'])&&!empty($_GET['id'])){
        $dom = new DomDocument();
        $dom->preserveWhiteSpace = false;  
        $dom->load("data.xml");
        $root = $dom->documentElement;
        $record = $root->getElementsByTagName("data");
        $ID=$root->getElementsByTagName("ID");
        $nodetoremove = null;
        //$namenode=$root->getElementsByTagName("own_name");
        //$name="";
        //$datenode=$root->getElementsByTagName("sign_in");
        //$date="";
        $newid=$_GET['id'];
        foreach($ID as $node){

        $pid =$node->textContent;

Here I am checking if it's a new ID and if it is it does the following as seen.

    if ($pid == $newid) 
            { 
$nodetoremove=$node->parentNode;

            } 

    }

The issue is here. I am able to go through the selected node I wish to delete ($nodetoremove) and select a specific element (sign_in) but I am unsure how to so. Right now all I can do is go through and print all of the elements within the nodes of $nodetoremove. Is there a way I can get the element I want from XML this way?

    //Prints all information within $nodetoremove
        foreach ($nodetoremove->childNodes AS $item){
        print $item->nodeName . "=" . $item->nodeValue . "<br>";
        }
    foreach ($nodetoremove as $node) {
 }
    //Sets $name to the first Child of $nodetoremove
    $name=$nodetoremove->firstChild->nodeValue;


    //Checks if the nods to remove is not null, if it is removes $nodetoremove
    if($nodetoremove!=null){
        $root->removeChild($nodetoremove);
?>
KidMessiah
  • 33
  • 4
  • Check `DOMXpath::evaluate()` – ThW Jul 27 '16 at 09:54
  • How would I apply that to my code? Sorry i'm kind of new to PHP. – KidMessiah Jul 29 '16 at 00:07
  • Xpath allows you to use expressions to fetch nodes from the DOM. Like SQL allows you to fetch fields from the database. Basically you can fetch node lists like with `getElementsByTagName`, but the expression can contain location paths and conditions. The second argument of `DOMXpath::evaluate()` is a context node. Your expression will be relative to it if provided. http://stackoverflow.com/questions/28371798/delete-a-node-of-an-xml-file-with-php/28412778#28412778 – ThW Jul 29 '16 at 07:55

0 Answers0