Results 1 to 15 of 16
-
May 3rd, 2009, 01:11 PM #15Registered User
postsUsing PHP to update an XML document
Hello,
I realise there is another thread for this, but it is slightly different to what I have here I thought it would be best to start fresh.
I am attempting to create a xml document for a flash music player, which can be updated using a php form. I've been at it for hours and hours, and have got a half working piece of code, mashed together from many different sources and websites.
My problem is that when I try add a new "song" using the form, it completely overwrites the entire xml document.
This is what I have so far:
XML Document: I have manually placed some data in there for testing purposes.
Form & current xml contents: The data displays from the xml document correctly.Code:<?xml version="1.0" encoding="utf-8"?> <musiclist> <song> <artist>Gandhi Bo-Bandhi</artist> <title>Gandhi Dub</title> <url>songs/GandhiDub.mp3</url> </song> <song> <artist>L-Wiz</artist> <title>Girl From Codeine City</title> <url>songs/02 Girl From Codeine City.mp3</url> </song> </musiclist>
The form action: - This is the root of the problem & I would like to know if I've approached it completely wrong, and if so, what needs to be done to fix it.PHP Code:<h1>Playlist</h1>
<form action="playlistaction.php" method="post">
<fieldset>
<label for="artist">Artist:</label><input type="text" id="artist" name="artist" /><br />
<label for="title">Title:</label> <input type="text" id="title" name="title"/><br />
<label for="path">URL:</label> <input type="text" id="url" name="url" /> <br />
<input type="submit" />
</fieldset>
</form>
<h2>Current entries:</h2>
<p>Artist - Title - URL</p>
<?php
$doc = new DOMDocument();
$doc->load( 'musiclist.xml' );
$musiclist = $doc->getElementsByTagName( "song" );
foreach( $musiclist as $song )
{
$artists = $song->getElementsByTagName( "artist" );
$artist = $artists->item(0)->nodeValue;
$titles= $song->getElementsByTagName( "title" );
$title= $titles->item(0)->nodeValue;
$urls = $song->getElementsByTagName( "url" );
$url = $urls->item(0)->nodeValue;
echo "<b>$artist - $title - $url\n</b><br>";
}
?>
So, instead of adding a new song at the end of the xml, the xml is completely wiped and replaced with the new data... Any ideas as to how I can fix this will be greatly apprecaited.PHP Code:<?php
$musiclist = array();
$musiclist [] = array(
"artist" => $_POST['artist'],
"title" => $_POST['title'],
"url" => $_POST['url']
);
$doc = new DOMDocument();
$doc->formatOutput = true;
$r = $doc->createElement( "musiclist" );
$doc->appendChild( $r );
foreach( $musiclist as $song )
{
$b = $doc->createElement("song");
$title = $doc->createElement("title");
$title->appendChild(
$doc->createTextNode( $song["title"] )
);
$b->appendChild( $title );
$artist = $doc->createElement("artist");
$artist->appendChild(
$doc->createTextNode( $song["artist"] )
);
$b->appendChild( $artist );
$url = $doc->createElement("url");
$url->appendChild(
$doc->createTextNode( $song["url"] )
);
$b->appendChild( $url );
$r->appendChild( $b );
}
echo $doc->saveXML();
$doc->save("musiclist.xml")
?>
-
May 3rd, 2009, 01:56 PM #2
I'm not sure what you're trying to loop across there with `foreach ($musiclist as $song)` -- that's not necessary to add one song. It's not necessary to rebuild the document from scratch, either.
All you need to do is load in the document, get the musiclist element and then add one new song element.
Hope that helpsPHP Code:$song = array(
'title' => $_POST['title'],
'artist' => $_POST['artist'],
'url' => $_POST['url'],
);
$doc = new DOMDocument();
$doc->load( 'musiclist.xml' );
$doc->formatOutput = true;
$r = $doc->getElementsByTagName("musiclist")->item(0);
$b = $doc->createElement("song");
$title = $doc->createElement("title");
$title->appendChild(
$doc->createTextNode( $song["title"] )
);
$b->appendChild( $title );
$artist = $doc->createElement("artist");
$artist->appendChild(
$doc->createTextNode( $song["artist"] )
);
$b->appendChild( $artist );
$url = $doc->createElement("url");
$url->appendChild(
$doc->createTextNode( $song["url"] )
);
$b->appendChild( $url );
$r->appendChild( $b );
$doc->save("musiclist.xml");
"60% of the time it works... every time." -- Paul Rudd as Brian Fantana.
-
May 3rd, 2009, 08:20 PM #35Registered User
postsThank you!
The code worked perfectly

I've modified it to support file uploads now, so the song is uploaded through the form, and the url is automatically added to the xml document.
I'll post the final code incase anyone needs it. Its not the tightest, or most secure code in the world, but its fine for what I need it for.
XML Document (This time with values inputed through the form)
Form (now with upload) & code to show entries already in the playlist.Code:<?xml version="1.0" encoding="utf-8"?> <musiclist> <song> <title>Gandhi Dub</title> <artist>Gandhi Bo-Bandhi</artist> <url>songs/GandhiDub.mp3</url> </song> <song> <title>Lost</title> <artist>Gandhi Bo-Bandhi</artist> <url>songs/lost.mp3</url></song> <song> <title>Hellhorn</title> <artist>Gandhi Bo-Bandhi</artist> <url>songs/HELLHORN.mp3</url> </song> </musiclist>
playlistaction.php (also stuck a header at the bottom so it takes you back to the previous page when its done)PHP Code:
<h1>Jukebox Playlist</h1>
<p>Enter the Artist and Title names as you want them to appear on the jukebox. It'll take a few minutes to upload a song after you click submit, so leave it and don't close the page until you see it added to the Current Entries list.</p>
<form action="playlistaction.php" method="post" enctype="multipart/form-data">
<table>
<tr>
<td colspan="2"class="labelcell"><label for="artist">Artist:</label></td>
<td colspan="2"class="fieldcell"><input type="text" id="artist" name="artist" tabindex="1"/></td>
</tr>
<tr>
<td colspan="2"class="labelcell"><label for="title">Title:</label></td>
<td colspan="2"class="fieldcell"> <input type="text" id="title" name="title" tabindex="2"/><br />
</td>
</tr>
<!-- <tr>
<td colspan="2"class="labelcell"><label for="path">URL:</label></td>
<td colspan="2"class="fieldcell"> <input type="text" id="url" name="url" tabindex="3"/> <br />
</td>
</tr>-->
<tr>
<td colspan="2"class="labelcell"><label for="userfile">Song Upload</label></td>
<td colspan="2"><input name="userfile" type="file" id="userfile" tabindex="4"></td></tr>
</td>
</tr>
<td colspan="4"><input type="submit" name="upload" class="box" value="Submit" tabindex="5" /></td>
</table>
</fieldset>
</form>
<h2>Current entries:</h2>
<p>Artist - Title - URL</p>
<?php
$doc = new DOMDocument();
$doc->load( 'musiclist.xml' );
$musiclist = $doc->getElementsByTagName( "song" );
foreach( $musiclist as $song )
{
$artists = $song->getElementsByTagName( "artist" );
$artist = $artists->item(0)->nodeValue;
$titles= $song->getElementsByTagName( "title" );
$title= $titles->item(0)->nodeValue;
$urls = $song->getElementsByTagName( "url" );
$url = $urls->item(0)->nodeValue;
echo "<b>$artist - $title - $url\n</b><br>";
}
?>
Thanks again for the help, and I hope this code saves people the trouble I had!PHP Code:<?php
$uploadDir = 'songs/';
if(isset($_POST['upload']))
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$filePath = $uploadDir . $fileName;
$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}
}
$song = array(
'title' => $_POST['title'],
'artist' => $_POST['artist'],
'url' => $filePath,
);
$doc = new DOMDocument();
$doc->load( 'musiclist.xml' );
$doc->formatOutput = true;
$r = $doc->getElementsByTagName("musiclist")->item(0);
$b = $doc->createElement("song");
$title = $doc->createElement("title");
$title->appendChild(
$doc->createTextNode( $song["title"] )
);
$b->appendChild( $title );
$artist = $doc->createElement("artist");
$artist->appendChild(
$doc->createTextNode( $song["artist"] )
);
$b->appendChild( $artist );
$url = $doc->createElement("url");
$url->appendChild(
$doc->createTextNode( $song["url"] )
);
$b->appendChild( $url );
$r->appendChild( $b );
$doc->save("musiclist.xml");
header("Location: {$_SERVER['HTTP_REFERER']}");
?>
-
May 3rd, 2009, 09:30 PM #4
That's very sporting of you. Glad you go it working
"60% of the time it works... every time." -- Paul Rudd as Brian Fantana.
-
July 28th, 2009, 07:16 AM #51Registered User
postsdone a pukka job with that i can't get my head round t to work on my set up ill show you the codes and the warning message but im doing something wrong but one good thng s that it s sort of working just not as good lol......bit of advice from someone would be nice ty

playlist.xml
<?xml version="1.0" encoding="utf-8"?>
<musiclist>
<track>
<path>samples/1.mp3</path>
<title>sample-1</title>
</track>
<track>
<path>samples/2.mp3</path>
<title>sample-2</title>
</track><track><path>samples/bomb-01.mp3</path><title>dddd</title></track><track><path>samples/bomb-01.mp3</path><title>dddd</title></track><track><path>samples/bomb-01.mp3</path><title>dddd</title></track><track><path>samples/air.rns</path><title>dddd</title></track><track><path>samples/bomb-01.mp3</path><title>tile</title></track><track><path>samples/evil jaide.gif</path><title>tile</title></track></musiclist>
(don't watch the uploads was testing it lol)
playlistaction.php
<?php
$uploadDir = 'samples/';
if(isset($_POST['upload']))
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$filePath = $uploadDir . $fileName;
$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}
}
$track = array(
'title' => $_POST['path'],
'path' => $filePath,
);
$doc = new DOMDocument();
$doc->load( 'playlist.xml' );
$doc->formatOutput = true;
$r = $doc->getElementsByTagName("musiclist")->item(0);
$b = $doc->createElement("track");
$path = $doc->createElement("path");
$path->appendChild(
$doc->createTextNode( $track["path"] )
);
$b->appendChild( $path );
$title = $doc->createElement("title");
$title->appendChild(
$doc->createTextNode( $track["title"] )
);
$b->appendChild( $title );
$r->appendChild( $b );
$doc->save("playlist.xml");
header("Location: {$_SERVER['HTTP_REFERER']}");
?>
<FORM><INPUT TYPE='button' VALUE='Back' onClick='history.go(-1);return true;'> </FORM>(lol had to put a back button)
uploadpage.php
<h3>upload music</h3>
<form action="playlistaction.php" method="post" enctype="multipart/form-data">
<table>
<tr>
<td colspan="2"class="labelcell"><label for="path">title:</label></td>
<td colspan="2"class="fieldcell"> <input type="text" id="path" name="path" tabindex="2"/><br />
</td>
</tr>
<!-- <tr>
<td colspan="2"class="labelcell"><label for="path">title:</label></td>
<td colspan="2"class="fieldcell"> <input type="text" id="title" name="title" tabindex="3"/> <br />
</td>
</tr>-->
<tr>
<td colspan="2"class="labelcell"><label for="userfile">track Upload</label></td>
<td colspan="2"><input name="userfile" type="file" id="userfile" tabindex="4"></td></tr>
</td>
</tr>
<td colspan="4"><input type="submit" name="upload" class="box" value="Submit" tabindex="5" /></td>
</table>
</form>
<h3>uploads:</h3>
<p>title ------------- path</p>
<?php
$doc = new DOMDocument();
$doc->load( 'playlist.xml' );
$musiclist = $doc->getElementsByTagName( "track" );
foreach( $musiclist as $track )
{
$artists = $track->getElementsByTagName( "artist" );
$artist = $artists->item(0)->nodeValue;
$paths= $track->getElementsByTagName( "title" );
$path= $paths->item(0)->nodeValue;
$titles = $track->getElementsByTagName( "path" );
$title = $titles->item(0)->nodeValue;
echo "<b>$path - $title\n</b><br>";
}
?>
now i seem to get a warning message "Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\playlistaction.php:5) in C:\wamp\www\playlistaction.php on line 54"
can anyone help fix my bug thank you
...
Last edited by darren1; July 28th, 2009 at 07:18 AM.
-
July 28th, 2009, 08:35 AM #6Let us live so that when we come to die even the undertaker will be sorry. - Mark Twain
Don't PM me your CSS, xHTML, JS or PHP questions. I will not reply to ANY IE6 questions.
-
October 8th, 2010, 05:51 AM #76Registered User
postsThanks a lot for sharing your hardwork.
This code is working perfectly. i want to know the delete code for the same.
if i want to delete some songs entries from the xml file then for that delete code is required.
and one more thing in this code when i add song it goes to the last. can we set the last added
entry at the first page?
Thanks a lot in advance.
Very nice code implemented.
Thanks
-
October 8th, 2010, 06:26 AM #8
Hi, pwsol. To insert to the top of a node list you'll need to use a combination of `insertBefore` and `$firstChild` in something like the following manner (I've not tested it):
And if you're wanting to delete elements, you'll need get all of the <song> elements and find the one that matches the credentials of what you want to delete. Get those elements using `$r->getElementsByTagName('song')` and loop through it in a `for(each)` loop, checking for the title/file within that element. If they're a match, remove the node using `$r->removeChild($song);`, `break;` and then save as before.PHP Code:if ($r->firstChild) {
$r->insertBefore($b, $r->firstChild);
} else {
$r->appendChild($b);
}
You can find all of the information that you need here: http://uk3.php.net/manual/en/book.dom.php
Hope that helps
"60% of the time it works... every time." -- Paul Rudd as Brian Fantana.
-
October 8th, 2010, 09:15 AM #96Registered User
postsif u kindly upload the code. whould be helpfull for me.
Hi thanks for quick reply...
well i have got this code for delete the xml entry but it has some error. kindly correct the error please.
<html>
<head></head>
<body>
<?php
$xmldoc = new DOMDocument();
$xmldoc->load('sample.xml', LIBXML_NOBLANKS);
$count = 0;
$activities = $xmldoc->firstChild->firstChild;
//prints the list of activities, with checkboxes on the left for each item
//the $count variable is the id to each entry
if($activities!=null){
echo '<form name=\'erase\' action=\'delete.php\' method=\'post\'>' . "\n";
while($activities!=null){
$count++;
echo " <input type=\"checkbox\" name=\"activity[]\" value=\"$count\"/>";
echo ' '.$activities->textContent.'<br/>'."\n";
$activities = $activities->nextSibling;
}
echo ' <input type=\'submit\' value=\'erase selected\'>';
echo '</form>';
}
?>
//section used for inserting new entries. this feature is working as expected.
<form name='input' action='insert.php' method='post'>
insert activity:
<input type='text name='activity'/>
<input type='submit' value='send'/>
<br/>
</form>
</body>
</html>
-
October 8th, 2010, 09:17 AM #106Registered User
posts
-
October 8th, 2010, 10:31 AM #11
Can you paste the contents of the script that actually tries to delete the XML node? Namely delete.php. Also, please surround your script in [php][/ php] tags (minus the space) so that it is formatted in a readable manner (as I have done with the code I pasted.)
"60% of the time it works... every time." -- Paul Rudd as Brian Fantana.
-
October 8th, 2010, 11:39 AM #126Registered User
posts
-
October 8th, 2010, 11:44 AM #136Registered User
postsi had taken this code from http://stackoverflow.com/questions/1...-using-php-dom
-
October 8th, 2010, 11:52 AM #14
Yes, I'm sure it would be more helpful if I were to build it on my own.
"60% of the time it works... every time." -- Paul Rudd as Brian Fantana.
-
October 9th, 2010, 01:42 AM #156Registered User
postsI will be thankful to you if u will make for me. and will be helpful to me like needy ppl
well from net i got the following delete code which works ok but
i want to delete the chide nodes but it deletes the main parent node.
<?php
function delete_book_id($id, $filename = 'loaned.xml'){
$data = simplexml_load_file($filename);
for($i = 0, $length = count($data->resource->book); $i < $length; $i++){
if($data->resource->book[$i]->ID == $id){
unset($data->resource->book[$i]);
break;
}
}
file_put_contents($filename, $data->saveXML());
}
//SAMPLE USAGE
delete_book_id('Book 1');
?>
xml file
----------
<catalogue>
<resource>
<book>
<ID>Book 1</ID>
<date>today</date>
<retdate>tomorrow</retdate>
<SID>Student 1</SID>
</book>
<book>
<ID>Book 2</ID>
<date>today</date>
<retdate>tomorrow</retdate>
<SID>Student 4</SID>
</book>
</resource>
</catalogue>
This delete php code works fine
but my requirement is to delete the chide node <ID>
but it deletes the all parent node <book>
my xml file is like this
slides>
<slide>
<url>images/simple_img_4.jpg</url>
</slide>
<slide>
<url>images/simple_img_4.jpg</url>
</slide>
I want the php code to delete the url node only
I will be thankful if anyone make the file
cheers.

Reply With Quote


Bookmarks