Im trying to edit an xml file using some old code I managed to dig up from another part of this forum. I have managed to get it to output the xml as I require however I am running into a few “Error parsing XML file” errors and consequently it will only add one entry to the xml file (prior to editing it could add multiple)
As I am new to parsing xml files I was hoping someone could have a quick look though my code and tell me where I may be going wrong as without it directing me to a particular line of code I have no idea where to start.
This is the index.php file:
<form action="processForm.php" method="post">
<fieldset>
<label for="name">Title:</label> <input type="text" id="name" name="name"/><br />
<label for="artist">Description:</label><input type="text" id="description" name="description" /><br />
<label for="path">Image:</label> <input type="text" id="image" name="image" /> <br />
<select name="action">
<option value="ins">Insert</option>
<option value="del">Delete</option>
</select>
<input type="submit" />
</fieldset>
</form>
<p>Current entries:</p>
<?php
function start_tag($parser, $name, $attrs){
global $table_string;
$table_string .= "<tr><td>$attrs[myTitle]</td><td>$attrs[myDescription]</td><td>$attrs[myMedia]</td></tr>";
}
function end_tag($parser, $name){}
$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_set_element_handler($parser, "start_tag", "end_tag");
$table_string = "<table>
<tr><th>Title</th><th>Description</th><th>Media</th></tr>";
xml_parse($parser, file_get_contents("images.xml")) or die("Error parsing XML file");
$table_string .= "</table>";
echo $table_string;
?>
Here is the ProcessForm.php
<?
$mySlideShow = Array();
function start_element($parser, $name, $attrs){
global $mySlideShow;
if($name == "myImage"){
array_push($mySlideShow, $attrs);
}
}
function end_element ($parser, $name){}
$playlist_string = file_get_contents("images.xml");
$parser = xml_parser_create();
xml_set_element_handler($parser, "start_element", "end_element");
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parse($parser, $playlist_string) or die("Error parsing XML document.");
print "<br />";
if($_POST['action'] == "ins"){
array_push($mySlideShow, Array(
"myTitle" => $_POST['name'],
"myDescription" => $_POST['description'],
"myMedia" => $_POST['image']));
$mySlideShow_final = $mySlideShow;
}else if($_POST['action'] == "del"){
$mySlideShow_final = Array();
foreach($mySlideShow as $myImage){
if($myImage['myTitle'] != $_POST['name']){
array_push($mySlideShow_final, $myImage);
}
}
}
$write_string = "<mySlideShow>";
foreach($mySlideShow_final as $myImage){
$write_string .= "<myImage>";
$write_string .= "<myTitle>$myImage[myTitle]</myTitle>";
$write_string .= "<myDescription>$myImage[myDescription]</myDescription>";
$write_string .= "<myMedia>$myImage[myMedia]</mymedia>";
$write_string .= "</myImage>";
}
$write_string .= "</mySlideShow>";
$fp = fopen("images.xml", "w+");
fwrite($fp, $write_string) or die("Error writing to file");
fclose($fp);
print "<em>Project inserted or deleted successfully :)</em><br />";
print "<a href=\"index.php\" title=\"return\">Return</a>";
?>
If anyone could have a look and point out where Im going wrong i would really appreciate it.
-Kirk