Jump to content

PHP DOM Help


Matty999555

Recommended Posts

I've been using SimpleXML for most of my xml functions but as it doesn't contain functions for deleting elements. I have come across problems such as :

 

Fatal error: Uncaught exception 'DOMException' with message 'Not Found Error'

 

PHP Code:

session_start(); 

if(!empty($_GET['delid']) && $_SESSION['login'] == "true" && !empty($_GET['article'])) { 
    if($_SESSION['level'] == "rootuser" || $_SESSION['level'] == "admin" || $_SESSION['level'] == "moderator") { 
        $comments['article'] = $_GET['article'] . ".xml"; 
        $doc  = new DOMDocument(); 
        $doc->load("../../xml/" . $comments['article'] . ".comments");  
        $count = 0; 
        $searchNodes = $doc->getElementsByTagName("comment")->item(0);  
        foreach($searchNodes as $searchNode) {  
            $check = preg_replace("/%20/", "", $_GET['delid']); 
            $valueID = $searchNode->getElementsByTagName("id")->item(0); 
            //if($valueID == $check) { 
                $doc = $doc->removeChild($searchNode); 
                $deleted = "deleted"; 
            //} 
            $count = $count + 1; 
        } 
        // Fine below 
        if($deleted == "deleted") { 
            $doc->save("../../xml/" . $comments['article'] . ".comments"); 
            header("Location: /error.php?title=Comment%20Deleted&msg=Comment%20Deleted"); 
        } else { 
            header("Location: /error.php?title=Error&msg=ID%20Does%20Not%20Exist!"); 
        } 
    } else { 
        header("Location: /error.php?title=Error!&msg=You%20do%20not%20have%20Permission%20to%20delete%20Comments!"); 
    } 
}  

My code is above and this is a sample file:

 

Code:

<?xml version="1.0"?>
<index>
<about>Comments Ver 1.0</about>
<comment><name>Matthew</name><email>n/a</email><website/><content>Content</content><time>Friday 18th of June 2010 08:23:40 AM</time><ip>n/a</ip><id>08:23:40, 18062010</id></comment></index>

It is meant to be access like this:

 

?delid=08:23:40,%2018062010&article=test

 

However I cannot find a working solution to my problem. I know that it it these two lines which are causing it.

 

$searchNodes = $doc->getElementsByTagName("comment")->item(0);

 

$valueID = $searchNode->getElementsByTagName("id")->item(0);

 

Can someone help me, thanks.

Link to comment
Share on other sites

Pretty sure that kind of error is in relation to removeChild. Hence, you're attempting to delete an element that does not exist, or so it seems. When does this happen? Also, is there any more to the error code? Such as line numbers, etc.

Link to comment
Share on other sites

Pretty sure that kind of error is in relation to removeChild. Hence, you're attempting to delete an element that does not exist, or so it seems. When does this happen? Also, is there any more to the error code? Such as line numbers, etc.

 

That happened a little while ago, it doesn't happen at the mo.

 

What I think is wrong Is that I am failing to properly locate the element so nothing is deleted.

 

Full code:

<?php

/*  COMMENT BLOCK
*   THIS IS THE SAME LENGTH AS IT WAS ORIGINALLY
*   I HAVE JUST REMOVED THE DETAILS
*
*
*
*
*
*
*
*
*
*
*
*
**********************************************************************************/

session_start();

if(!empty($_GET['delid']) && $_SESSION['login'] == "true" && !empty($_GET['article'])) {
if($_SESSION['level'] == "rootuser" || $_SESSION['level'] == "admin" || $_SESSION['level'] == "moderator") {
	$comments['article'] = $_GET['article'] . ".xml";
	$doc  = new DOMDocument();
	$doc->load("../../xml/" . $comments['article'] . ".comments"); 
	$count = 0;
	$searchNodes = $doc->getElementsByTagName("comment")->item(0); 
	foreach($searchNodes as $searchNode) { 
		$check = preg_replace("/%20/", "", $_GET['delid']);
		$valueID = $searchNode->getElementsByTagName("id")->item(0);
		//if($valueID == $check) {
			$doc = $doc->removeChild($searchNode);
			$deleted = "deleted";
		//}
		$count = $count + 1;
	}
	// Fine below
	if($deleted == "deleted") {
		$doc->save("../../xml/" . $comments['article'] . ".comments");
		header("Location: /error.php?title=Comment%20Deleted&msg=Comment%20Deleted");
	} else {
		header("Location: /error.php?title=Error&msg=ID%20Does%20Not%20Exist!");
	}
} else {
	header("Location: /error.php?title=Error!&msg=You%20do%20not%20have%20Permission%20to%20delete%20Comments!");
}
} elseif(!empty($_GET['article'])) {
if(preg_match("/^[a-zA-Z0-9_-]*$/", $_GET['article'])) {
if(preg_match("/^[a-zA-Z0-9_-\s]*$/", $_POST['name'])) {
if(preg_match("/^[a-zA-Z0-9_-\s\[\],.:;\"'\(\)\\/]*$/", $_POST['comment'])) {
if(preg_match("(\w+@[a-zA-Z_]+?\.[a-zA-Z])", $_POST['email']) || $_POST['email'] == "userloggedinnoneedtocheck") {

	$comments['article'] = htmlentities($_GET['article']) . ".xml";
	$comments['name'] = htmlentities($_POST['name']);
	$comments['email'] = htmlentities($_POST['email']);
	$comments['comment'] = htmlentities($_POST['comment']);
	if(preg_match("([http://]\w+.+w|[http://www.]\w+.+w|[http://]\w+.+w+.+w|[http://www.]\w+.+w+.+w)", $_POST['website']) || empty($_POST['website'])) {
		$comments['website'] = htmlentities($_POST['website']);
	}
$comments['time'] = date('l jS \of F Y h:i:s A');
$ipadress   = $_SERVER['REMOTE_ADDR'];
$returnlink = $_SERVER['HTTP_REFERER']; 
if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['comment'])) {
	header("Location: /error.php?title=Error&msg=You%20didn't%20fill%20in%20a%20required Field!");
} else {
	if(file_exists("../../xml/" . $comments['article'] . ".comments")) {
		$doc  = new DOMDocument();
		$doc->load("../../xml/" . $comments['article'] . ".comments");
		$root = $doc->getElementsByTagName("index")->item(0);
		$doc->appendChild($root);
		$comment = $doc->createElement("comment");
		$root->appendChild($comment);
			$element = $doc->createElement("name");
			$element->appendChild($doc->createTextNode($comments['name']));
			$comment->appendChild($element);
			$element = $doc->createElement("email");
			$element->appendChild($doc->createTextNode($comments['email']));
			$comment->appendChild($element);
			$element = $doc->createElement("website");
			$element->appendChild($doc->createTextNode($comments['website']));
			$comment->appendChild($element);
			$element = $doc->createElement("content");
			$element->appendChild($doc->createTextNode($comments['comment']));
			$comment->appendChild($element);
			$element = $doc->createElement("time");
			$element->appendChild($doc->createTextNode($comments['time']));
			$comment->appendChild($element);
			$element = $doc->createElement("ip");
			$element->appendChild($doc->createTextNode($ipadress));
			$comment->appendChild($element);
			$element = $doc->createElement("id");
			$element->appendChild($doc->createTextNode(date("H:i:s, dmY")));
			$comment->appendChild($element);
		$commentfile = file_get_contents("../../xml/" . $comments['article'] . ".comments");
		if(strpos($_SERVER["REMOTE_ADDR"], $commentfile)) {
			header("Location: /error.php?title=Error&msg=You%20have%20already%20posted%20a%20Message!");
		}
		$doc->save("../../xml/" . $comments['article'] . ".comments");
		header("Location: /error.php?title=Comments%20Saved&msg=Thanks%20for%20submitting%20a%20comment");
	} else {
	header("Location: /error.php?title=Error&msg=Comments%20are%20not%20enabled%20for%20this%20page!%20Please%20update%20links");
	}
}

} else {header("Location: /error.php?title=Invalid Email!&msg=The%20email%20entered%20was%20invalid!");}
} else {header("Location: /error.php?title=Invalid%20Comment!&msg=You%20put%20a%20forbidden%20character%20in%20the%20message!");}
} else {header("Location: /error.php?title=Invalid%20Name!&msg=You%20didn't%20fill%20in%20your%20name%20Correctly!");}
} else {header("Location: /error.php?title=Invalid%20ID!&msg=We're%20sorry%20to%20say%20that%20the%20ID%20was%20Invalid");}

} else {
header("Location: /index.php");
}
die("ERROR");
?>

 

I can think of a different way to do this. I'm going to try in a sec

Link to comment
Share on other sites

I think this is wrong:

 

$searchNodes = $doc->getElementsByTagName("comment")->item(0); 

 

It should be

 

$searchNodes = $doc->getElementsByTagName("comment"); 

 

EDIT

 

Tried this:

		$count = 0;
	$searchNodes = $doc->getElementsByTagName("id"); 
	foreach($searchNodes as $searchNode) { 
		$check = preg_replace("/%20/", "", $_GET['delid']);
		if($searchNode == $check) {
			$remove = $doc->getElementsByTagName("comment")->item($count);
			$doc = $doc->removeChild($remove);
			$deleted = "deleted";
		}
		$count = $count + 1;
	}

 

Didn't work

 

Link to comment
Share on other sites

Well, I spent over an hour trying to figure this out, and I think I've finally figured it out.

 

<?php

if(!empty($_GET['delid']) && $_SESSION['login'] == "true" && !empty($_GET['article'])) { 
    if($_SESSION['level'] == "rootuser" || $_SESSION['level'] == "admin" || $_SESSION['level'] == "moderator") { 
        $comments['article'] = $_GET['article'] . ".xml"; 
        $doc  = new DOMDocument(); 
        $doc->load("../../xml/" . $comments['article'] . ".comments");  
        $searchNodes = $doc->getElementsByTagName("comment");
        $domElemsToRemove = array(); 
        foreach($searchNodes as $searchNode) {  
            $check = preg_replace("/%20/", "", $_GET['delid']);
            $valueID = $searchNode->getElementsByTagName("id")->item(0)->nodeValue;
           
            if($valueID == $check) { 
			$domElemsToRemove[] = $searchNode;
            } 
        } 
        
        foreach( $domElemsToRemove as $domElement ){
		$domElement->parentNode->removeChild($domElement);
		$deleted = "deleted";
	} 

        if($deleted == "deleted") { 
            $doc->save("../../xml/" . $comments['article'] . ".comments"); 
            header("Location: /error.php?title=Comment%20Deleted&msg=Comment%20Deleted"); 
        } else { 
         header("Location: /error.php?title=Error&msg=ID%20Does%20Not%20Exist!"); 
        } 
    } else { 
        header("Location: /error.php?title=Error!&msg=You%20do%20not%20have%20Permission%20to%20delete%20Comments!"); 
    } 
}  

?>

 

First of all:

 

if($valueID == $check)

 

$valueID was not a string, and thus could not be compared to one, so:

 

$valueID = $searchNode->getElementsByTagName("id")->item(0)->nodeValue;

 

nodeValue had to be added in order to get the text value of the ID and compare it to $check.

 

Secondly, to remove the child successfully, I had to put it in an array then later use foreach() to delete it.

 

Make sure that the XML file has proper file permissions to be modified and such.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.