Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a PHP Expert!

Join 244,291 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 945 people online right now. Registration is fast and FREE... Join Now!




Writing to the middle of an xml page

 
Reply to this topicStart new topic

Writing to the middle of an xml page

Lydon
13 Nov, 2008 - 03:46 PM
Post #1

New D.I.C Head
*

Joined: 22 May, 2008
Posts: 22


My Contributions
Hi all

I'm currently messing about trying to populate an XML file from data entered in a form. I've already started off the XML file with the XML version, the RSS version and I've managed to get it so that all data entered in the form, is then sent to a database and is written to my XML file, each set of data following the last. Perfect.

Now, I'm wondering how you get to close off the XML file with </rss> when each time I add something via the form, its going to be inserted at the end of the file.

Here's the code i have at present.

CODE
<?php
    // Gather details from form
    $newHeadline = $_POST['headline'];
    $newDate = $_POST['date'];
    $newURL = $_POST['url'];
    $newDescription = $_POST['description'];
    $newArticle = $_POST['article'];
    
    // Create INSERT query for new data into News table
    $query = "INSERT INTO news (headline, date, url, description, article) VALUES ('$newHeadline', '$newDate', '$newURL', '$newDescription', '$newArticle')";
    
    // Run query through connection
    $result = mysql_query ($query, $connection);
    
    //$filename = 'results.xml';
    
    $fp = fopen('results.xml', 'a');
    fwrite($fp, "\n" . '<item>' . "\n");
    fwrite($fp, '<title>');
    fwrite($fp, $newHeadline);
    fwrite($fp, '</title>' . "\n");
    fwrite($fp, '<pubDate>');
    fwrite($fp, $newDate);
    fwrite($fp, '</pubDate>' . "\n");
    fwrite($fp, '<link>');
    fwrite($fp, $newURL);
    fwrite($fp, '</link>' . "\n");
    fwrite($fp, '<description>');
    fwrite($fp, $newDescription);
    fwrite($fp, '</description>' . "\n");
    fwrite($fp, '<article>');
    fwrite($fp, $newArticle);
    fwrite($fp, '</article>' . "\n");
    fwrite($fp, '</item>' . "\n");
    fclose($fp);
    
        ?>


And here is the XML file which keeps getting the form data added to.

CODE
<?xml version="1.0" ?>
<rss version="2.0">
  <channel>
    <title></title>
    <link>/</link>
    <description></description>
    <image>
        <url></url>
        <link></link>
    </image>
<item>
<title>nearly there</title>
<pubDate>Thur, 13 Nov 2008 22:57</pubDate>
<link>http://www.somethinghere.com</link>
<description>almost...</description>
<article>just need to figure out how to close off this file.</article>
</item>


So as you can see, everything between the <item></item> keeps getting added each time the form is filled in, but how on earth do i get the final </channel> and </rss> ???

Any help would be much appreciated. Thanks



User is offlineProfile CardPM
+Quote Post


AdaHacker
RE: Writing To The Middle Of An Xml Page
14 Nov, 2008 - 03:19 PM
Post #2

D.I.C Regular
***

Joined: 17 Jun, 2008
Posts: 285



Thanked: 51 times
My Contributions
The answer is simple: don't append to the file. Appending to the file writes data at the end, and you want to write your data before the end, so that's not what you want. So you need to open the file for reading and writing ("r+"), seek to the correct position, write your data (overwriting the closing tags), and then write new closing tags.
User is offlineProfile CardPM
+Quote Post

CTphpnwb
RE: Writing To The Middle Of An Xml Page
14 Nov, 2008 - 03:51 PM
Post #3

D.I.C Lover
Group Icon

Joined: 8 Aug, 2008
Posts: 1,570



Thanked: 94 times
Dream Kudos: 100
Expert In: PHP

My Contributions
This has nothing to do with writing in the middle, but your code above can be shortened by concatenating everything to one command. It should run a bit faster too.
CODE
$data= '\n<item>\n<title>'.$newHeadline.'</title>\n<pubDate>'.$newDate'</pubDate>\n<link>'.$newURL.'</link>\n<description>'.$newDescription.'</description>\n<article>'.$newArticle.'</article>\n</item>\n';
$fp = fopen('results.xml', 'a');
fwrite($fp, $data);
fclose($fp);


User is offlineProfile CardPM
+Quote Post

Lydon
RE: Writing To The Middle Of An Xml Page
14 Nov, 2008 - 04:02 PM
Post #4

New D.I.C Head
*

Joined: 22 May, 2008
Posts: 22


My Contributions
Awesome. After changing from append it works great now, thanks AdaHacker biggrin.gif
Here's the code I now have:

CODE
$fp = fopen('results.xml', 'r+');
    fseek($fp, -17, SEEK_END);
    fwrite($fp, "\n" . '<item>' . "\n");
    fwrite($fp, '<title>');
    fwrite($fp, $newHeadline);
    fwrite($fp, '</title>' . "\n");
    fwrite($fp, '<pubDate>');
    fwrite($fp, $newDate);
    fwrite($fp, '</pubDate>' . "\n");
    fwrite($fp, '<link>');
    fwrite($fp, $newURL);
    fwrite($fp, '</link>' . "\n");
    fwrite($fp, '<description>');
    fwrite($fp, $newDescription);
    fwrite($fp, '</description>' . "\n");
    fwrite($fp, '<article><![CDATA[');
    fwrite($fp, $newArticle);
    fwrite($fp, ']]></article>' . "\n");
    fwrite($fp, '</item>' . "\n");
    fwrite($fp, '</channel></rss>');
    fclose($fp);


Also, thanks to CTphpnwb, i'll check out that shortened form now. Cheers!
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 03:55PM

Live PHP Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month