XML Parse Error won't leave me alone
Posted: Thu Dec 03, 2009 8:55 am
Hello,
I got this code off a forum and edited it to my preferences. The only problem is now it doesn't work. The original code used and output an xml file like this:
Old db.xml
Thats fine, if you want to use attributes, I however do not.
Here are the 3 codes I have starting with my new db.xml file, then the htm form page, then the php code used to do the work.
db.xml
ii.htm
processform23.php
Im assuming that it has something to do with the xml parser at the beginning and the array_push($stores, Array(. The code will add the new entry but you will get a parse error when it tries to pull all the other entries and put them back in the file. Just copy and paste the code to see what I mean. I have the latest version of WAMP d/l'ed and installed, php 5 of course. Any help would be greatly appreciated as I am new to php and this is the only code ill need to work with for the next 6 months or so.
Thanks in advance,
mmof
I got this code off a forum and edited it to my preferences. The only problem is now it doesn't work. The original code used and output an xml file like this:
Old db.xml
Code: Select all
<stores>
<store title="example" artist="example" path="example" />
<store title="example" artist="example" path="example" />
</stores>
Here are the 3 codes I have starting with my new db.xml file, then the htm form page, then the php code used to do the work.
db.xml
Code: Select all
<stores>
<store>
<title>"Example1" </title>
<artist>"Example1"</artist>
<path>"Example1"</path>
</store>
<store>
<title>"Example2" </title>
<artist>"Example2"</artist>
<path>"Example2"</path>
</store>
</stores>
Code: Select all
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form action="processForm23.php" method="post">
<label for="name">Name:</label> <input type="text" id="name" name="name"/><br />
<label for="artist">Artist:</label><input type="text" id="artist" name="artist" /><br />
<label for="path">Path:</label> <input type="text" id="path" name="path" /> <br />
<select name="action">
<option value="ins">Insert</option>
<option value="del">Delete</option>
</select>
<input type="submit" />
</form>
</body>
</html>
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Adding Stores...</title>
<style type="text/css">
em {
text-align:center;
}
</style>
</head>
<body>
<p>
<?php
$stores = Array();
function start_element($parser, $name, $attrs){
global $stores;
if($name == "store"){
array_push($stores, $attrs);
}
}
function end_element ($parser, $name){}
$stores_string = file_get_contents("db.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, $stores_string) or die("Error parsing XML document.");
print "<br />";
if($_POST['action'] == "ins"){
array_push($stores, Array(
"title" => $_POST['name'],
"artist" => $_POST['artist'],
"path" => $_POST['path']));
$stores_final = $stores;
}else if($_POST['action'] == "del"){
$stores_final = Array();
foreach($stores as $store){
if($store['title'] != $_POST['name']){
array_push($stores_final, $store);
}
}
}
$write_string = "<stores>\n";
foreach($stores_final as $store){
$write_string .= "<store>\n";
$write_string .= "<title>\"$store[title]\" </title>\n";
$write_string .= "<artist>\"$store[artist]\"</artist>\n";
$write_string .= "<path>\"$store[path]\" </path>\n";
$write_string .= "</store>\n";
}
$write_string .= "</stores>";
$fp = fopen("db.xml", "w+");
fwrite($fp, $write_string) or die("Error writing to file");
fclose($fp);
print "<b>Store inserted or deleted successfully!</b><br />";
print "<a href=\"ii.htm\" title=\"return\">Return</a>";
?>
</p>
</body>
</html>
Thanks in advance,
mmof