TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 01-18-2008, 01:42 AM   #1 (permalink)
The Acquainted
 
buildakicker's Avatar
 
Join Date: Jan 2008
Posts: 119
Thanks: 21
buildakicker is on a distinguished road
Terminal Writing to XML file

Hi All,

I was originally going to use .txt files, then thought it'd be way better to use .xml files. So, now I have to write to them. My issue is, how to read between the "lines". Right now I have:

PHP Code:
if(isset($_POST['newpost'])){
    
//get the info from the form, post to text file.
    
$title stripslashes($_POST['title']);
    
$category stripslashes($_POST['category']);
    
$body stripslashes($_POST['body']);
    
    
//add data XML style
    
$data "<post>" "\n" "<category>"$category "</category>" "\n" "<title>" $title "</title>" "\n" "<date>" $todaysdate "</date>" "\n" "<body>" "\n"$body "</body>" "\n" "</post>""\n\r";
    
    
//add data to CSV Text File
    //$data = $category . "||" . $title . "||" . $body . "\n";
    //open file and store contents
    
$file fopen('posts.xml','a+');
    if (!
$file) {echo 'ERROR: Unable to open file.'; exit;} 
    
//post current post, then write $currentdata back to file
    // Write the data to the file
    
fwrite($file$data);
    
// Close the file
    
fclose($file);
    
    
$postupdated "You have created a new post!";

...which posts fine to the file. However, if I have:

Code:
<?xml version="1.0" standalone="yes"?>
<posts>
  <post>
    <title>Some people like meat.</title>
    <category>Meat</category>
    <pubDate>2008-January-15</pubDate>
  </post>
</posts>
What is the best way to get the <posts> and then write to it without messing the xml file up?
Code:
<?xml version="1.0" standalone="yes"?>
<posts>
     //insert $data here
</posts>
What I am thinking is I should get all the <post> data from the XML file and store it in $dataxml then add $data to it, then write it back to the file.

Would that be the best way?

Thanks
__________________
SkiLeases.com
buildakicker is offline  
Reply With Quote
Old 01-18-2008, 01:59 AM   #2 (permalink)
The Acquainted
 
buildakicker's Avatar
 
Join Date: Jan 2008
Posts: 119
Thanks: 21
buildakicker is on a distinguished road
Default

Welp, Icanhavethattookaboutasecond. I am still not exactally sure how to get stuff from between the lines...

PHP Code:
if(isset($_POST['newpost'])){
    
//get the info from the form, post to text file.
    
$title stripslashes($_POST['title']);
    
$category stripslashes($_POST['category']);
    
$body stripslashes($_POST['body']);
    
    
//add data XML style
    
$data "<post>" "\n" "<category>"$category "</category>" "\n" "<title>" $title "</title>" "\n" "<date>" $todaysdate "</date>" "\n" "<body>" "\n"$body "</body>" "\n" "</post>""\n\r";
    
    
//add data to CSV Text File
    //$data = $category . "||" . $title . "||" . $body . "\n";
    //open file and store contents
    
$file fopen('posts.xml','a+');
    if (!
$file) {echo 'ERROR: Unable to open file.'; exit;} 
    
//post current post to $data, get $dataxml from file, then write $alldata back to file
    
while (!feof($file)) {
        
$dataxml fgets($file);
    }
    
$alldata $dataxml.$data;
    
// Write the data to the file
    
fwrite($file$alldata);
    
// Close the file
    
fclose($file);
    
    
$postupdated "You have created a new post!";

__________________
SkiLeases.com
buildakicker is offline  
Reply With Quote
Old 01-18-2008, 02:22 AM   #3 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

SimpleXML can actually create XML documents. However, it's not that great in my opinion - with its lack of support for CDATA blocks and no removeNode function, it leaves a lot to be desired. I prefer DOM Document which also allows the creation of XML documents. Unfortunately, we don't have any articles really on this topic - not in any depth anyway, but here's a good old Google search that yields several good results by the looks of it.

If you have any questions when reading those articles - just ask!
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 01-18-2008, 04:15 AM   #4 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

i can think of 2 posts that you may find useful although they both kinda require some XML background knowledge:
Generating XML from a Mysql DB with PHP's DOM functions (part one)

TalkPHP - Creating RSS documents with the DOM API

yea the php dom functions are really the best and indeed the easiest way to create XML docs in my opinion.

this should hopefully get you started:
PHP Code:
$pDOMdoc = new DomDocument();

$pDOMdoc->load('xmldoc.xml');//load your exsisting xml doc

//get the posts node in the xml

$postsNode $pDOMdoc->getElementsByTagName('posts')->item(0);

//create new post add it to <posts>
$postNode $pDOMdoc->createElement('post');
$postNode $postsNode->appendChild($postNode);

//creae title node add to <post>
$titleNode $pDOMdoc->createElement('title');
$titleNode $postNode->appendChild($titleNode);
//add text node to the new title node
$titleTextNode $pDOMdoc->createTextNode($title);
$titleTextNode $titleNode->appendChild($titleTextNode);

//create catagory node add it to <post>
$catNode $pDOMdoc->createElement('catagory');
$catNode $postNode->appendChild($catNode);
//add text node to catagory node
$catTextNode $pDOMdoc->createTextNode($catagory);
$catTextNode $catNode->appendChild($catTextNode);

$pubDateNode $pDOMdoc->createElement('pubDate');
$pubDateNode $postNode->appendChild($pubDateNode);
$pubDateTextNode $pDOMdoc->createTextNode($date);
$pubDateTextNode $pubDateNode->appendChild($pubDateTextNode);


echo 
$pDOMdoc->save('xmldoc.xml'); 
this is based off the xml tree you posted:

<?xml version="1.0" standalone="yes"?>
<posts>
<post>
<title>Some people like meat.</title>
<category>Meat</category>
<pubDate>2008-January-15</pubDate>
</post>
</posts>
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)

Last edited by sketchMedia : 01-18-2008 at 08:56 AM. Reason: managed mispell 'text' 3 or 4 times
sketchMedia is offline  
Reply With Quote
Old 01-18-2008, 06:30 PM   #5 (permalink)
The Acquainted
 
buildakicker's Avatar
 
Join Date: Jan 2008
Posts: 119
Thanks: 21
buildakicker is on a distinguished road
Default

Thanks for the responces. I found an article and have followed it, however, it's overwriting my data, not appending to it.

Here's my form post code:
PHP Code:
<?php
if(isset($_POST['newpost'])){
    
//get the info from the form, post to text file.
    
$ptitle stripslashes($_POST['title']);
    
$pcategory stripslashes($_POST['category']);
    
$pbody stripslashes($_POST['body']);

//################### XML #################################    

 //Creates XML string and XML document using the DOM 
 
$dom = new DomDocument('1.0'); 
 
//add root - <posts> 
 
$posts $dom->appendChild($dom->createElement('posts')); 
 
//add <post> element to <posts> 
 
$post $posts->appendChild($dom->createElement('post')); 
 
//add <title> element to <post> 
 
$title $post->appendChild($dom->createElement('title')); 
  
//add <category> element to <post> 
 
$category $post->appendChild($dom->createElement('category')); 
  
//add <body> element to <post> 
 
$body $post->appendChild($dom->createElement('body')); 
  
//add <pubDate> element to <post> 
 
$pubDate $post->appendChild($dom->createElement('pubDate')); 
 
//add <title> text node element to <title> 
 
$title->appendChild
                 
$dom->createTextNode($ptitle)); 
 
$category->appendChild
                 
$dom->createTextNode($pcategory)); 
 
$body->appendChild
                 
$dom->createTextNode($pbody)); 
 
$pubDate->appendChild
                 
$dom->createTextNode($todaysdate)); 
 
//generate xml 
 
$dom->formatOutput true// set the formatOutput attribute of 
                            // domDocument to true 
 // save XML as string or file 
 
$test1 $dom->saveXML(); // put string in test1 
 
$dom->save('posts.xml'); // save as file 

########## XML END ##########################################
    
$postupdated "You have created a new post!";
}
?>
Do I need to store the current file in a variable somehow?
__________________
SkiLeases.com
buildakicker is offline  
Reply With Quote
Old 01-18-2008, 06:59 PM   #6 (permalink)
The Addict
Top Contributor Good Samaritan 
 
Join Date: Jan 2008
Location: USA
Posts: 217
Thanks: 16
RobertK is on a distinguished road
Default

You need to load from file, then append your elements. If you look at the code you create a new document and then save it. Of course it overwrites the old during this process.
__________________
Programmers are in a race with the Universe to create bigger and better idiot-proof programs, while the Universe is trying to create bigger and better idiots. So far the Universe is winning. - Rich Cook
RobertK is offline  
Reply With Quote
Old 01-18-2008, 07:39 PM   #7 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

yes, load an xmlfile into the dom as robertk says, it does most of the hard work for you :)
PHP Code:
$dom DomDocument();
$dom->loadXML('xmlfile'); 
at the moment its creating a new xml document then saving it over you old one, instead you need to tell it you have an exsiting tree and that you want to append changes to it, not create a new xml doc.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 02-05-2008, 10:35 PM   #8 (permalink)
The Acquainted
 
buildakicker's Avatar
 
Join Date: Jan 2008
Posts: 119
Thanks: 21
buildakicker is on a distinguished road
Default

Hello all,

So I have loaded the XML file into the dom, however, each time it still overwrites it with this code:

PHP Code:
 $dom = new DomDocument('1.0'); 
 
$dom -> loadXML('posts.xml'); 
I have tried this:

PHP Code:
 $dom DomDocument(); 
 
$dom -> loadXML('posts.xml'); 
...but it doesn't like it...

Here is my whole dom xml creator... I think it is almost there... just the overwrite problem. I don't understand that.
PHP Code:
//################### XML #################################    

 //Creates XML string and XML document using the DOM 
 
$dom DomDocument(); 
 
$dom -> loadXML('posts.xml');
 
//add root - <rss>
 
$rss $dom->appendChild($dom->createElement('rss')); 
 
//add <channel>  element to <RSS>
 
$channel $rss->appendChild($dom->createElement('channel')); 
 
//add <item> element to <channel> 
 
$item $channel->appendChild($dom->createElement('item')); 
 
//add <title> element to <item> 
 
$title $item->appendChild($dom->createElement('title')); 
  
//add <body> element to <item> 
 
$description $item->appendChild($dom->createElement('description')); 
   
//add <category> element to <item> 
 
$category $item->appendChild($dom->createElement('category')); 
  
//add <pubDate> element to <item> 
 
$pubDate $item->appendChild($dom->createElement('pubDate')); 
   
//add <link> element to <item> 
 
$link $item->appendChild($dom->createElement('link')); 
   
//add <author> element to <item> 
 
$author $item->appendChild($dom->createElement('author')); 
 
//add <title> text node element to <title> 
 
$title->appendChild
                 
$dom->createTextNode($ptitle)); 
 
$description->appendChild
                 
$dom->createTextNode($pdescription)); 
 
$category->appendChild
                 
$dom->createTextNode($pcategory)); 
 
$pubDate->appendChild
                 
$dom->createTextNode($todaysdate));
 
$link->appendChild
                 
$dom->createTextNode($plink)); 
 
$author->appendChild
                 
$dom->createTextNode($pauthor)); 
 
 
//generate xml 
 
$dom->formatOutput true// set the formatOutput attribute of 
                            // domDocument to true 
 // save XML as string or file 
 
$test1 $dom->saveXML(); // put string in test1 
 
$dom->save('posts.xml'); // save as file 

########## XML END ########################################## 
__________________
SkiLeases.com
buildakicker is offline  
Reply With Quote
Old 02-06-2008, 08:17 PM   #9 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

PHP Code:
$rss $dom->getElementsByTagName('rss')->item(0); 
use that to get your root node within your existing XML, then append changes to it, like in my previous post.

you also need the new keyword:
PHP Code:
$dom DomDocument(); 
Should be:
PHP Code:
$dom = new DomDocument(); 
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 11:08 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design