Page 1 of 1

XML Parse Error won't leave me alone

Posted: Thu Dec 03, 2009 8:55 am
by mmof
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

Code: Select all

 
<stores>
<store title="example" artist="example" path="example" />
<store title="example" artist="example" path="example" />
</stores>
 
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

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>
 
ii.htm

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>
 
processform23.php

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>
 
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

Re: XML Parse Error won't leave me alone

Posted: Thu Dec 03, 2009 3:37 pm
by cpetercarter
Remove the quotation marks in db.xml.

Re: XML Parse Error won't leave me alone

Posted: Thu Dec 03, 2009 4:26 pm
by mmof
I removed the quote marks in db.xml still no luck. The php code puts quotes in there too.

Re: XML Parse Error won't leave me alone

Posted: Thu Dec 10, 2009 9:07 pm
by mmof
I've figured it out! Or atleast a way to make it work. First off we need to create 2 new xml files:

db.xml

Code: Select all

 
<stores>
</stores>
 
and...
db2.xml

Code: Select all

 
<stores>
</stores>
 
Now we have the form that goes with it all we need is the new processform23.php code.
processform23.php

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>Managing 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 title=\"$store[title]\" artist=\"$store[artist]\" path=\"$store[path]\" />\n";
}
$write_string .= "</stores>";
$fp = fopen("db.xml", "w+");
fwrite($fp, $write_string) or die("Error writing to file");
fclose($fp);
$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("db2.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=\"sstest.html\" title=\"return\">Return</a>";
?>
</p>
</body>
</html>
 
And thats it! Hope this helps and if anyone knows of a better more efficient way let me know. Always open to help or critique.
Thanks, mmof