View Full Version : PHP writing to XML
simplistik
June 6th, 2006, 04:06 PM
So... I've scoured the internet and did a search on these forums... and perhaps I'm searching with the wrong terms... but here's what I'm tryin to do. I have a PHP document that pull a list of images and info from specified XML docs. Now I'm trying to create a client-side GUI (just a basic form) that will allow someone add another node to an the XML document it's pulling from.
Oh and let it first be known that I don't have the option to use a SQL Database... or any other type of Database... so XML is the best alternative.
My XML is formatted like any other XML doc:
<images>
<image>
<group>GROUP NAME</group>
<filename>FILE NAME.JPG</filename>
<title>TITLE</title>
<published>06/05/06</published>
</image>
</images>
the attached gif I removed the file names and what not... for confidentiality :P but be assured that it does publish properly.
λ
June 6th, 2006, 04:10 PM
http://www.kirupa.com/forum/showthread.php?t=51044&highlight=edit+xml
Have you seen that thread?
bwh2
June 6th, 2006, 04:12 PM
so you want to append the file? check out the php page for fwrite() (http://us2.php.net/fwrite) and do a find for "append".
simplistik
June 6th, 2006, 04:14 PM
:lol: i just realized my stupid search function was on a 1wk timeframe....
@bwh2 yea, I knew about fwrite() but I need a physical example of how it works as I'm not a "developer" myself... I try to keep my nose outta it.
@λ is there a way to do that without using the attributes?
simplistik
June 6th, 2006, 04:22 PM
I think I may have figured it out... I'll keep ya posted on my noobishness :P
Thx fellas
simplistik
June 6th, 2006, 05:37 PM
Yea ok... so maybe I don't have it figured out :lol: I did what I thought would need to be done and it doesn't do what I thought it should do :P ;)
JoshuaJonah
June 6th, 2006, 05:40 PM
well, post more questions and maybe us code monkeys can help you. (just be easy on us in the critiques forums:D)
simplistik
June 6th, 2006, 05:48 PM
well, post more questions and maybe us code monkeys can help you. (just be easy on us in the critiques forums:D)
:lol: well if one of you wants to jump on to MSN and hook a brotha up that'd be great... I tried to add you defective but you never accepted my invitation...
you can add me if you want though... it's not the MSN in my profile though it's btp@mydomainname.com (mydomainname is my domain name :P not "mydomainname")
bwh2
June 6th, 2006, 05:51 PM
[looks at defective's footer, looks at simp's posts, laughs heartily]. anyway, i'll take a look at this stuff when i get home if you guys don't already have it figured out.
simplistik
June 6th, 2006, 05:56 PM
[looks at defective's footer, looks at simp's posts, laughs heartily]. anyway, i'll take a look at this stuff when i get home if you guys don't already have it figured out.
:lol: yea, well I have no choice in the matter :P I'm against making things harder than what they should be :lol:
simplistik
June 6th, 2006, 06:10 PM
oh also... this has to be written in SAX not DOM :hugegrin:
simplistik
June 7th, 2006, 09:50 AM
ok fellas... so I'm modifying it and I get this error now:
Warning: fopen(xmlDB/recent.xml): failed to open stream: Permission denied in /Users/lucaswilliams/Sites/csd/references/playlist/processForm.php on line 30
Warning: fwrite(): supplied argument is not a valid stream resource in processForm.php on line 30
Error writing to file
on line 30 all I have...
"group" => $_POST['group'],
the code following that is
"group" => $_POST['group'],
"title" => $_POST['title'],
"name" => $_POST['name'],
"date" => $_POST['date'],
"url" => $_POST['url']));
anyway... here's the files... hook a brotha up and I may think of easing up on your critiques :evil: :lol:
bwh2
June 7th, 2006, 10:13 AM
drop your functions to the end of the php. then only look at the php, not the html. then you will see how fopen is on line 30.
getting to my question from earlier, are you trying to append the file or rewrite the whole thing? if you use w+, you will be rewriting it. you might want to use 'a+' (http://us2.php.net/function.fopen) if you intend on appending it.
simplistik
June 7th, 2006, 10:44 AM
I yea I just want to append, I think the reason he has it as w+ is because in that particular one he gives the option to delete an entry.
Anyway... I have no clue what you mean by
drop your functions to the end of the php. then only look at the php, not the html. then you will see how fopen is on line 30.
Gonna have to put it in dumb ppl terms :P ... remember
if ( me == designer && me != coder/developer ) {
me.gotoAndStop("nearest developer and ask questions cause I'm a noob");
}
when I move the functions below print right before the closing ?> I get this error
Warning: fopen(xmlDB/recent.xml): failed to open stream: Permission denied in processForm.php on line 26
Warning: fwrite(): supplied argument is not a valid stream resource in processForm.php on line 26
Error writing to file
and line 26 is just the start of the whole
"group" => $_POST['group'],
"title" => $_POST['title'],
"name" => $_POST['name'],
"date" => $_POST['date'],
"url" => $_POST['url']));
I've also noticed that it seems to echo a blank string right before it starts echoing the XML content... how can I stop that?
bwh2
June 7th, 2006, 10:58 AM
I yea I just want to append, I think the reason he has it as w+ is because in that particular one he gives the option to delete an entry.ah yes, that would make sense.
i just meant that when you're trying to figure out php parsing errors, your line count actually starts when the php starts. so the php line count starts at $songs = Array();. then php skips your functions when counting lines, then comes back to them once the non-function lines are done. i was just trying to explain how the parser came up with line 30. not a big deal here b/c you could identify where the fopen was, but more helpful in more verbose scripts.
I've also noticed that it seems to echo a blank string right before it starts echoing the XML content... how can I stop that?without being able to test it, i would try removing the print "<br />" in line 7 of your php. only seems logical.
you might want to try changing:
$fp = fopen("xmlDB/recent.xml", "w+");to
$xmlFile = "xmlDB/recent.xml";
chmod($xmlFile,0777)
$fp = fopen($xmlFile, "w+");
simplistik
June 7th, 2006, 11:19 AM
Hmnn... well that got rid of those two errors and now produced:
Parse error: parse error in processForm.php on line 26
Which is the end of the array mentioned above.
As for that print "<br/>" though that's i the processForm.php but that's not where the blank string is echoing. It's actually echoing in the "admin.php" or any place you attempt to call back the XML for display. So that error resides somewhere in...
<?php
function start_tag($parser, $name, $attrs){
global $table_string;
$table_string .= "<tr><td>$attrs[group]</td><td>$attrs[title]</td><td>$attrs[name]</td><td>$attrs[date]</td><td>$attrs[url]</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>GROUP</th><th>COLLECTION TITLE</th><th>FILENAME</th><th>DATE</th><th>URL</th></tr>";
xml_parse($parser, file_get_contents("xmlDB/recent.xml")) or die("Error parsing XML file");
$table_string .= "</table>";
echo $table_string;
?>
bwh2
June 7th, 2006, 12:06 PM
processForm.php:
<!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 Songs...</title>
<style type="text/css">
em {
text-align:center;
}
</style>
</head>
<body>
<p>
<?
$songs = Array();
function start_element($parser, $name, $attrs){
global $songs;
if($name == "song"){
array_push($songs, $attrs);
}
}
function end_element ($parser, $name){}
$xmlFile = 'xmlDB/recent.xml';
$playlist_string = file_get_contents($xmlFile);
$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($songs, Array(
"group" => $_POST['group'],
"title" => $_POST['title'],
"name" => $_POST['name'],
"date" => $_POST['date'],
"url" => $_POST['url']
));
$songs_final = $songs;
}else if($_POST['action'] == "del"){
$songs_final = Array();
foreach($songs as $song){
if($song['groupID'] != $_POST['group']){
array_push($songs_final, $song);
}
}
}
$write_string = '<songs>';
foreach($songs_final as $song){
$write_string .= '<song group="'.$song[groupID].'" title="'.$song[title].'" name="'.$song[name].'" date="'.$song[date].'" url="'.$song[url].'" />';
}
$write_string .= '</songs>';
chmod($xmlFile,0777);
$fp = fopen($xmlFile, '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="admin.php" title="return">Return</a>';
?>
</p>
</body>
</html>
btw, your group isn't inserting b/c you have "groupID" but your form and array say "group". replacing groupId with group will get it to work.
bwh2
June 7th, 2006, 12:16 PM
btw, if you want to sexify your xml doc with nice tabs and lines, change the middle section to include appropriate "\t" and "\r\n" like so:
$write_string = '<songs>'."\r\n";
foreach($songs_final as $song){
$write_string .= ."\t".'<song group="'.$song[group].'" title="'.$song[title].'" name="'.$song[name].'" date="'.$song[date].'" url="'.$song[url].'" />'."\r\n";
}
$write_string .= '</songs>'."\r\n";
//edit: in that same spirit, let's tighten admin.php...
<!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">
body { font: 62.5%/1.6em "Lucida Grande", Arial, Verdana, sans-serif; }
table {
border:1px dotted #ccc;
width:100%;
}
td {
background-color:#aaa;
color:#fff;
}
fieldset { border:0; }
label { display: block; }
</style>
</head>
<body>
<form action="processForm.php" method="post">
<fieldset>
<label for="group">GROUP:</label> <input type="text" id="group" name="group"/><br />
<label for="title">COLLECTION TITLE:</label><input type="text" id="title" name="title" /><br />
<label for="name">FILENAME:</label> <input type="text" id="name" name="name" /> <br />
<label for="date">PUBLISH DATE:</label> <input type="text" id="date" name="date" /> <br />
<label for="url">DOWNLOAD URL:</label> <input type="text" id="url" name="url" /> <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 .= "\t".'<tr>'."\r\n";
$table_string .= "\t\t".'<td>'.$attrs[group].'</td>'."\r\n\t\t".'<td>'.$attrs[title].'</td>'."\r\n\t\t".'<td>'.$attrs[name].'</td>'."\r\n\t\t".'<td>'.$attrs[date].'</td>'."\r\n\t\t".'<td>'.$attrs[url].'</td>'."\r\n";
$table_string .= "\t".'</tr>'."\r\n";
}
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>'."\r\n";
$table_string .= "\t".'<tr>'."\r\n\t\t".'<th>GROUP</th>'."\r\n\t\t".'<th>COLLECTION TITLE</th>'."\r\n\t\t".'<th>FILENAME</th>'."\r\n\t\t".'<th>DATE</th>'."\r\n\t\t".'<th>URL</th>'."\r\n\t".'</tr>'."\r\n";
xml_parse($parser, file_get_contents("xmlDB/recent.xml")) or die("Error parsing XML file");
$table_string .= '</table>'."\r\n";
echo $table_string;
?>
</body>
</html>
simplistik
June 7th, 2006, 12:55 PM
:lol: I just came here to tell you that I figured it out... I had my bleepin xml wrong... the xml file I was trying to write to still had the first nodes as <songs> when i changed it to try and read <images> as soon as I did that it was submitting fine. But then again I did start over from the base file again so who knows... maybe I missed something the first 1000 times.
The reason I had groupID and group is cause of that one line from the original that was pushing the arrays only if names != file or something like that. But I just deleted it too cause I didn't figure it needed that.
thanks for the xml formatting thing... it looks much prettier now :lol: I knew about those but I guess it never clicked in my tiny brain that I should've added them.
now... the last issue is gettin that is... that it still adds that empty string at the very top of the table in this one... in my other ones it adds an image w/ all blank values.
bwh2
June 7th, 2006, 01:04 PM
now... the last issue is gettin that is... that it still adds that empty string at the very top of the table in this one... in my other ones it adds an image w/ all blank values.
change
$table_string .= "\t".'<tr>'."\r\n";
$table_string .= "\t\t".'<td>'.$attrs[group].'</td>'."\r\n\t\t".'<td>'.$attrs[title].'</td>'."\r\n\t\t".'<td>'.$attrs[name].'</td>'."\r\n\t\t".'<td>'.$attrs[date].'</td>'."\r\n\t\t".'<td>'.$attrs[url].'</td>'."\r\n";
$table_string .= "\t".'</tr>'."\r\n";
to
if( count($attrs)>0 ) {
$table_string .= "\t".'<tr>'."\r\n";
$table_string .= "\t\t".'<td>'.$attrs[group].'</td>'."\r\n\t\t".'<td>'.$attrs[title].'</td>'."\r\n\t\t".'<td>'.$attrs[name].'</td>'."\r\n\t\t".'<td>'.$attrs[date].'</td>'."\r\n\t\t".'<td>'.$attrs[url].'</td>'."\r\n";
$table_string .= "\t".'</tr>'."\r\n";
}in admin.php
simplistik
June 7th, 2006, 01:11 PM
very nice... I got lost in all your "\t".'<tr> stuff... so I just grabbed the if statement and wrapped it around the code I already have :lol:. You the man though :thumb2: thanks for all the help... don't go very far though cause I'm expanding this and I may or maynot need help... :lol: never know. But it won't be this problem again :hugegrin:
bwh2
June 7th, 2006, 01:18 PM
no problem, glad to help. the only change was the if statement, so don't worry about the tab inserts.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.