Article
Build an XML-Based Content Management System with PHP
Editing an XML Article
Generally speaking, editing an XML article is very much the same as creating an article, except that you have to load an existing article's nodes into the edit form, then write your changes out to the file.
In this example, we're not going to allow any changes to the article's id attribute, as this would make it very difficult for the rest of the application to function properly. Restricting changes to the id attribute also allows us to perform an easy short cut when updating an XML file—all we have to do is delete the old file and create a new file with our new information. This is crude, but fast and effective.
Here is the code for the editArticle.php page. Note that the form's action is set to updateArticle.php. Also note the use of the extractText() function to extract content from an XML object and place it into a variable that can then be assigned to a form element.
<?php
session_start();
function extractText($array){
if(count($array) <= 1){
//we only have one tag to process!
for ($i = 0; $i<count($array); $i++){
$node = $array[$i];
$value = $node->get_content();
}
return $value;
}
}
if ($_SESSION["login"] != "true"){
header("Location:login.php");
$_SESSION["error"] = "<font color=red>You don't have privileges to see the admin page.</font>";
exit;
}
//pull in the XML file
if ($file == ""){
echo "<h2>You didn't choose a file to edit!</h2>";
echo "<a href=\"adminindex.php\">Go back to index and choose a file</a>";
exit;
} else {
$filename = "./xml/".$file;
$xml = domxml_open_file($filename);
$root = $xml->root();
$id = $root->get_attribute("id");
$h_array = $root->get_elements_by_tagname("headline");
$headline = extractText($h_array);
$stat_array = $root->get_elements_by_tagname("status");
$status = extractText($stat_array);
$a_array = $root->get_elements_by_tagname("author");
$author = extractText($a_array);
$e_array = $root->get_elements_by_tagname("email");
$email = extractText($e_array);
$ab_array = $root->get_elements_by_tagname("abstract");
$abstract = extractText($ab_array);
$kl_array = $root->get_elements_by_tagname("keywords");
$keywords = extractText($kl_array);
$lead_array = $root->get_elements_by_tagname("para-intro");
$plead = extractText($lead_array);
$second_array = $root->get_elements_by_tagname("para-main");
$pmain = extractText($second_array);
$con_array = $root->get_elements_by_tagname("para-conclusion");
$pcon = extractText($con_array);
$statusList = array("live","in progress");
?>
<html>
<title>Edit an XML Article</title>
<body>
<h1>Edit an XML Article</h1>
<a href="adminindex.php">Cancel</a><br><br>
<form name="createArticle" action="updateArticle.php" method="post">
<table border=1 cellspacing=0 cellpadding=3>
<tr valign=top>
<td width="135">Article ID</td>
<td width="634"> <?php echo htmlspecialchars($id); ?> <input type="hidden" name="id" value="<?php echo $id; ?>">
</td>
</tr>
<tr valign=top>
<td>Status</td>
<td>
<select name="status">
<?php
foreach ($statusList as $stat){
if($stat == $status){
echo "<option value=\"".$stat."\" selected>$stat";
} else {
echo "<option value=\"".$stat."\">$stat";
}
}
?>
</select>
</td>
</tr>
<tr valign=top>
<td>Headline</td>
<td> <input name="headline" type="text" id="headline" value="<?php echo htmlspecialchars($headline); ?>" size="60"></td>
</tr>
<tr valign=top>
<td>Author Name</td>
<td> <input name="name" type="text" id="name" value="<?php echo htmlspecialchars($author); ?>"size="30"></td>
</tr>
<tr valign=top>
<td>Author Email</td>
<td> <input name="email" type="text" id="email" value="<?php echo htmlspecialchars($email); ?>"size="30"></td>
</tr>
<tr valign=top>
<td>Keywords</td>
<td> <input name="keywords" type="text" value="<?php echo htmlspecialchars($keywords); ?>">
<br> <font size="-1">(separate keywords with commas)</font> </td>
</tr>
<tr valign=top>
<td>Abstract</td>
<td><textarea name="abstract" cols="50" rows="5" id="abstract"><?php echo htmlspecialchars($abstract); ?></textarea></td>
</tr>
<tr valign=top>
<td> <p>Article Body<br>
</p></td>
<td> <p>Intro paragraph:</p>
<p>
<textarea name="body[intro]" cols="70" rows="10" wrap="soft" id="body[intro]"><?php echo htmlspecialchars($plead); ?></textarea>
</p>
<p>Main paragraph:</p>
<p>
<textarea name="body[main]" cols="70" rows="10" wrap="soft" id="body[main]" ><?php echo htmlspecialchars($pmain); ?></textarea>
</p>
<p>Conclusion paragraph:</p>
<p>
<textarea name="body[conclusion]" cols="70" rows="10" wrap="soft"><?php echo htmlspecialchars($pcon); ?></textarea>
</p></td>
</tr>
<tr valign=top>
<td colspan=2> <div align="center">
<input type="submit" name="Add Article" value="Add Article">
<input name="reset" type="reset" id="reset" value="Reset">
</div></td>
</tr>
</table>
</form>
</body>
</html>
<?php
}//end if-else
?>
The updateArticle.php page is very similar to the addArticle.php page, but there's no need to check for a unique id attribute at the front. Also, at the end, the PHP code will delete the existing file and then dump the new XML information structure into a newly created file name with the same name. This saves us a lot of time trying to insert edits into the proper nodes.
<?php
//create document root
$doc = domxml_new_doc("1.0");
$root = $doc->create_element("article");
$root = $doc->append_child($root);
//add ID attribute
$root->set_attribute('id', $id);
//create headline
$head = $doc->create_element("headline");
$head = $root->append_child($head);
$htext = $doc->create_text_node($headline);
$htext = $head->append_child($htext);
//create author name
$aname = $doc->create_element("author");
$aname = $root->append_child($aname);
$atext = $doc->create_text_node($name);
$atext = $aname->append_child($atext);
//create author email
$mail = $doc->create_element("email");
$mail = $root->append_child($mail);
$mtext = $doc->create_text_node($email);
$mtext = $mail->append_child($mtext);
//create abstract
$abs = $doc->create_element("abstract");
$abs = $root->append_child($abs);
$abstext = $doc->create_text_node($abstract);
$abstext = $abs->append_child($abstext);
//create keyword listing
$keylisting = $doc->create_element("keywords");
$keylisting = $root->append_child($keylisting);
$ktext = $doc->create_text_node($keywords);
$ktext = $keylisting->append_child($ktext);
//create status, always in progress when first created
$stat = $doc->create_element("status");
$stat = $root->append_child($stat);
$stat_text = $doc->create_text_node($status);
$stat_text = $stat->append_child($stat_text);
//create paras
if (is_array($body)){
foreach ($body as $K => $V){
if ($V != ""){
$V = stripslashes($V);
$para = $doc->create_element("para-$K");
$para = $root->append_child($para);
$ptext = $doc->create_text_node($V);
$ptext = $para->append_child($ptext);
}
}
}
//write to the file (first delete existing one!)
$filename = "./xml/".$id . ".xml";
unlink($filename);
$doc->dump_file($filename, false, true);
//send user back to adminindex
header("Location:adminindex.php");
?>