TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Writing to XML file (http://www.talkphp.com/general/1994-writing-xml-file.html)

buildakicker 01-18-2008 01:42 AM

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

buildakicker 01-18-2008 01:59 AM

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!";



Wildhoney 01-18-2008 02:22 AM

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!

sketchMedia 01-18-2008 04:15 AM

i can think of 2 posts that you may find useful although they both kinda require some XML background knowledge:
http://www.talkphp.com/xml-xslt-xpat...-part-one.html

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>

buildakicker 01-18-2008 06:30 PM

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?

RobertK 01-18-2008 06:59 PM

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. ;-)

sketchMedia 01-18-2008 07:39 PM

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.

buildakicker 02-05-2008 10:35 PM

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 ########################################## 


sketchMedia 02-06-2008 08:17 PM

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(); 



All times are GMT. The time now is 10:07 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0