Anybody know how to fix this?

Hello guys. Great forum, not just that but also members.

So I used one old script to load xml file from php form, posted long time ago here, and spend last weekend to made this work good, but somewhere I had success, somewhere don’t.

So I hoping some here have solution. For rest of the people who need this I will explaine in short what I have.

So this xml file I need to edit test.xml. Also it is possible that I have that in one line.


<?xml version="1.0" encoding="utf-8"?>
<playlist version="1" xmlns="http://xspf.org/ns/0/">
<trackList>
<track>
<title>Example 1</title>
<creator>Joe Tribiani</creator>
<location>/roki/images/6c.png</location>
<info>index.php</info>
</track>
<track>
<title>Example 2</title>
<creator>Joe Tribiani</creator>
<location>/roki/images/27.png</location>
<info>forum</info>
</track>
</trackList>
</playlist>

This is my PHP processform.php


<?
$trackList = Array();
function start_element($parser, $name, $attrs){
global $trackList;
if($name == "track"){
array_push($trackList, $attrs);
}
}
function end_element ($parser, $name){}
$playlist_string = file_get_contents("test.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($trackList, Array(
"title" => $_POST['title'],
"creator" => $_POST['creator'],
"location" => $_POST['location'],
"info" => $_POST['info']));
$trackList_final = $trackList;
}else if($_POST['action'] == "del"){
$trackList_final = Array();
foreach($trackList as $track){
if($track['title'] != $_POST['title']){
array_push($trackList_final, $track);
}
}
}
//Write XML vers and enctype
$write_string .= '<?xml version="1.0" encoding="utf-8"?>';
// Write root tag open tag <root>
$write_string .= '<playlist version="1" xmlns="http://xspf.org/ns/0/">';
$write_string .= '<trackList>';
$write_string .= '<track>';
foreach($trackList_final as $track)
{
$write_string .= '<title>'.$track[title].'</title>';
$write_string .= '<creator>'.$track[creator].'</creator>';
$write_string .= '<location>'.$track[location].'</location>';
$write_string .= '<info>'.$track[info].'</info>';

}
 
// Write root tag close tag </root>
$write_string .= '</track>';
$write_string .= '</trackList>';
$write_string .= '</playlist>';
$fp = fopen("test.xml", "w+");
fwrite($fp, $write_string) or die("Error writing to file");
fclose($fp);
print "<em>Song inserted or deleted successfully :)<em><br />";
print "<a href=\"index.php\" title=\"return\">Return</a>";
?>

When I first time update test.xml with processForm.php, my test.xml is OK. I get what I need just everything is one line. No problem. Problem start when I do that second time. Than values what added first time dissapered and I have only in test.xml values from second update.

So test.xml look like this.


<?xml version="1.0" encoding="utf-8"?><playlist version="1" xmlns="http://xspf.org/ns/0/"><trackList><track><title></title><creator></creator><location></location><info></info><title>Example 2</title><creator>JoeTribiani</creator><location>/roki/images/27.png</location><info>forum</info></track></trackList></playlist>

Any help?

Thanx

Also for those who need this, this is my form php file called index.php. I moded this file from original version, so at the bottom he show excatly how test.xml file look like.


<!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>Add Songs</title>
<style type="text/css">
table {
border:1px dotted #ccc;
width:100%;
}
td {
background-color:#aaa;
color:#fff;
}
fieldset { border:0; }
label { margin-right:5px; }
</style>
</head>
<body>
<form action="processForm.php" method="post">
<fieldset>
<label for="title">Name:</label> <input type="text" id="title" name="title"/><br />
<label for="creator">Creator:</label><input type="text" id="creator" name="creator" /><br />
<label for="location">Location:</label> <input type="text" id="location" name="location" /> <br />
<label for="info">Info:</label> <input type="text" id="info" name="info" /> <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
$file = "test.xml";
function trustedFile($file) 
{
// only trust local files owned by ourselves
if (!eregi("^([a-z]+)://", $file) 
&& fileowner($file) == getmyuid()) {
return true;
}
return false;
}
function startElement($parser, $name, $attribs) 
{
echo "&lt;<font color=\"#0000cc\">$name</font>";
if (count($attribs)) {
foreach ($attribs as $k => $v) {
echo " <font color=\"#009900\">$k</font>=\"<font 
color=\"#990000\">$v</font>\"";
}
}
echo "&gt;";
}
function endElement($parser, $name) 
{
echo "&lt;/<font color=\"#0000cc\">$name</font>&gt;";
}
function characterData($parser, $data) 
{
echo "<b>$data</b>";
}
function PIHandler($parser, $target, $data) 
{
switch (strtolower($target)) {
case "php":
global $parser_file;
// If the parsed document is "trusted", we say it is safe
// to execute PHP code inside it. If not, display the code
// instead.
if (trustedFile($parser_file[$parser])) {
eval($data);
} else {
printf("Untrusted PHP code: <i>%s</i>", 
htmlspecialchars($data));
}
break;
}
}
function defaultHandler($parser, $data) 
{
if (substr($data, 0, 1) == "&" && substr($data, -1, 1) == ";") {
printf('<font color="#aa00aa">%s</font>', 
htmlspecialchars($data));
} else {
printf('<font size="-1">%s</font>', 
htmlspecialchars($data));
}
}
function externalEntityRefHandler($parser, $openEntityNames, $base, $systemId,
$publicId) {
if ($systemId) {
if (!list($parser, $fp) = new_xml_parser($systemId)) {
printf("Could not open entity %s at %s
", $openEntityNames,
$systemId);
return false;
}
while ($data = fread($fp, 4096)) {
if (!xml_parse($parser, $data, feof($fp))) {
printf("XML error: %s at line %d while parsing entity %s
",
xml_error_string(xml_get_error_code($parser)),
xml_get_current_line_number($parser), $openEntityNames);
xml_parser_free($parser);
return false;
}
}
xml_parser_free($parser);
return true;
}
return false;
}
function new_xml_parser($file) 
{
global $parser_file;
$xml_parser = xml_parser_create();
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 1);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
xml_set_processing_instruction_handler($xml_parser, "PIHandler");
xml_set_default_handler($xml_parser, "defaultHandler");
xml_set_external_entity_ref_handler($xml_parser, "externalEntityRefHandler");
if (!($fp = @fopen($file, "r"))) {
return false;
}
if (!is_array($parser_file)) {
settype($parser_file, "array");
}
$parser_file[$xml_parser] = $file;
return array($xml_parser, $fp);
}
if (!(list($xml_parser, $fp) = new_xml_parser($file))) {
die("could not open XML input");
}
echo "<pre>";
while ($data = fread($fp, 4096)) {
if (!xml_parse($xml_parser, $data, feof($fp))) {
die(sprintf("XML error: %s at line %d
",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
}
echo "</pre>";
xml_parser_free($xml_parser);
?> 
 
 
 
 
</body>
</html>

Ok, its getting rid of the previous data because its just writing over it, not adding to it.

in php, its called “Append”

instead of


$fp = fopen("test.xml", "w+");

use this


$fp = fopen("test.xml", 'a');

hope it helps

[quote=strokemymouse;2327875]Ok, its getting rid of the previous data because its just writing over it, not adding to it.

in php, its called “Append”

instead of


$fp = fopen("test.xml", "w+");

use this


$fp = fopen("test.xml", 'a');

hope it helps[/quote]

Thank you for the replay.

Lets see.

test.xml file after FIRST update it is OK.


[SIZE=-1]<?xml version="1.0" encoding="utf-8"?><playlist version="1" xmlns="http://xspf.org/ns/0/"><trackList><track><title>Example 1</title><creator>Joe</creator><location>/roki/images/test1.jpg</location><info>forum</info></track></trackList></playlist>
[/SIZE]

After second update it is not OK, so it is not OK. Thanx strokemymouse.


[SIZE=-1]<?xml version="1.0" encoding="utf-8"?><playlist version="1" xmlns="http://xspf.org/ns/0/"><trackList><track><title>Example 1</title><creator>Joe</creator><location>/roki/images/test1.jpg</location><info>forum</info></track></trackList></playlist><?xml version="1.0" encoding="utf-8"?><playlist version="1" xmlns="http://xspf.org/ns/0/"><trackList><track><title></title><creator></creator><location></location><info></info><title>Example 2</title><creator>Joe</creator><location>/roki/images/test2.jpg</location><info>index.php</info></track></trackList></playlist>
[/SIZE][SIZE=3]

[/SIZE]

I think main problem here is somewhere in this code.

//Write XML vers and enctype
$write_string .= '<?xml version="1.0" encoding="utf-8"?>';
// Write root tag open tag <root>
$write_string .= '<playlist version="1" xmlns="http://xspf.org/ns/0/">';
$write_string .= '<trackList>';
$write_string .= '<track>';
foreach($trackList_final as $track)
{
$write_string .= '<title>'.$track[title].'</title>';
$write_string .= '<creator>'.$track[creator].'</creator>';
$write_string .= '<location>'.$track[location].'</location>';
$write_string .= '<info>'.$track[info].'</info>';
 
}
 
// Write root tag close tag </root>
$write_string .= '</track>';
$write_string .= '</trackList>';
$write_string .= '</playlist>';
$fp = fopen("test.xml", "w+");

I think i can shed light on this once again :slight_smile:

This line:


$write_string .= '<?xml version="1.0" encoding="utf-8"?>';

replace with:

$write_string .= ‘<?xml version=“1.0” encoding=“utf-8”?>’;

edit// I could be wrong once again…
You need to parse out any " or ’ if your using them inside a string because php will think its the end of the string.

[quote=strokemymouse;2328201]I think i can shed light on this once again :slight_smile:

This line:


$write_string .= '<?xml version="1.0" encoding="utf-8"?>';

replace with:

$write_string .= ‘<?xml version=\"1.0\" encoding=\"utf-8\"?>’;

edit// I could be wrong once again…
You need to parse out any " or ’ if your using them inside a string because php will think its the end of the string.[/quote]I don’t have anything against. I was spend all weekend on this, and I have feeling I am near solution, so I don’t have anything against proposals.

No, it is not that. I deleted completely that line, becouase my flash is able to work without that, so my xml look like this to work:


<playlist>
<trackList>
 
<track>
<title>Example1</title>
<creator>Joe Tribiani</creator>
<location>images/example1.gif</location>
<info>../index.php</info>
</track>
 
<track>
<title>Example2</title>
<creator>Joe Tribiani</creator>
<location>images/example2.gif</location>
<info>../article.php</info>
</track>
 
</trackList>
</playlist>

Now I was try a lot of the options, and best what I came up is this.


<?
$trackList = Array();
function start_element($parser, $name, $attrs){
global $trackList;
if($name == "track"){
array_push($trackList, $attrs);
}
}
function end_element ($parser, $name){}
$trackList_string = file_get_contents("test.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, $trackList_string) or die("Error parsing XML document.");
print "<br />";
 
if($_POST['action'] == "ins"){
array_push($trackList, Array(
"title" => $_POST['title'],
"creator" => $_POST['creator'],
"location" => $_POST['location'],
"info" => $_POST['info']));
$trackList_final = $trackList;
}else if($_POST['action'] == "del"){
$trackList_final = Array();
foreach($trackList as $track){
if($track['title'] != $_POST['title']){
array_push($trackList_final, $track);
}
}
}
 
// Write root tag open tag <root>
$write_string .= '<playlist>';
$write_string .= '<trackList>';
foreach($trackList_final as $track)
{
$write_string .= '<track>';
$write_string .= '<title>'.$track[title].'</title>';
$write_string .= '<creator>'.$track[creator].'</creator>';
$write_string .= '<location>'.$track[location].'</location>';
$write_string .= '<info>'.$track[info].'</info>';
$write_string .= '</track>';
}
 
// Write root tag close tag </root>
$write_string .= '</trackList>';
$write_string .= '</playlist>';
$fp = fopen("test.xml", "w+"); 
fwrite($fp, $write_string) or die("Error writing to file");
fclose($fp);
print "<em>Song inserted or deleted successfully :)<em><br />";
print "<a href=\"index.php\" title=\"return\">Return</a>";
?>

With thois form test.xml look like this.


<playlist>
<trackList>
 
<track>
<title></title>
<creator></creator>
<location></location>
<info></info>
</track>
 
<track>
<title>fsdf</title>
<creator>sdfsd</creator>
<location>qsfds</location>
<info>dsfsd</info>
</track>
 
</trackList>
</playlist>

Of course all of this is in one line. I put them in more lines.
So if I add values via processForm.php 3 times, just first track will have values, and all others will be without values. If I change “w+” for “a”, than I have error in xml file.

Also I will try to parse any " and ’ and I will test that.