Write XML code in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Largo65
Forum Newbie
Posts: 4
Joined: Fri Mar 07, 2008 4:44 am

Write XML code in PHP

Post by Largo65 »

Hi, simple question: How do I create a PHP variable with this data: <?xml version="1.0"?><Tracks></Tracks>
so that in the "Overwrite file with new data" in my script, PHP will write this XML-data variable instead of the $raw_xml variable in my XML file?
One of the problems I ran into when trying this myself was that the PHP will not let my use the "" when I create a variable, but I need these quote symbols for the XML ... :!:
Can any one help me? :)

Code: Select all

<?php 
//This PHP script writes data that is sent from flash, into an XML file:------------------------------------------------
$filename = "xmldata/trackingData.xml"; 
$raw_xml = file_get_contents("php://input"); 
 
print $raw_xml; 
 
//Check File Size. The size returned is in BYTES 
$size = filesize($filename); 
 
//If XML file is larger than 50KB's then make a copy. 
if($size > 51200){ 
     
    //Copy file with new name containing the day it was copied 
    $today = date("Ymd"); 
    $newFile = "xmldata/trackingData".$today.".xml"; 
    copy($filename, $newFile); 
   //-------------------------------------------------------------------------!!!!!!!!!!!!!!!!!!
   //Overwrite file with new data. -----------------------------!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    $fp = fopen($filename, "w"); 
    fwrite($fp, $raw_xml); //How do I put the my XML code here?  -------------------!!!!!!!!!!
    fclose($fp); 
 
 
} else { 
    //Write normally 
    $fp = fopen($filename, "w"); 
    fwrite($fp, $raw_xml); 
    fclose($fp); 
} 
?>
anto91
Forum Commoner
Posts: 58
Joined: Mon Mar 10, 2008 10:59 am
Location: Sweden

Re: Write XML code in PHP

Post by anto91 »

Use single quotes to avoid error messaging when writing regular quotes in a varible.

Example

Code: Select all

 
<?php
echo '<xml tag="foo">bar</xml>';
?>
 
Largo65
Forum Newbie
Posts: 4
Joined: Fri Mar 07, 2008 4:44 am

Re: Write XML code in PHP

Post by Largo65 »

anto91 wrote:Use single quotes to avoid error messaging when writing regular quotes in a varible.

Example

Code: Select all

 
<?php
echo '<xml tag="foo">bar</xml>';
?>
 
This works perfectly!:D Thanks anto91 :D
anto91
Forum Commoner
Posts: 58
Joined: Mon Mar 10, 2008 10:59 am
Location: Sweden

Re: Write XML code in PHP

Post by anto91 »

The reason what you were trying to do was breaking the actual code

Example

Code: Select all

 
<?php
// Php thinks you ended the tag there
echo "<xml tag=" 
 
// But you continue with a value
echo "<xml tag="value"></xml>";
?>
 
Php thinks you ended the echo then comes a bunch of stuff it dose not know how to handle.

Other way of doing it

Code: Select all

 
<?php
echo "<xml tag=\"value\">sfoo</xml>";
?>
 
That is both uglier and slower and contains more characters to write.
User avatar
Sekka
Forum Commoner
Posts: 91
Joined: Mon Feb 18, 2008 10:25 am
Location: Huddersfield, West Yorkshire, UK

Re: Write XML code in PHP

Post by Sekka »

anto91 wrote:Other way of doing it

Code: Select all

 
<?php
echo "<xml tag=\"value\">sfoo</xml>";
?>
 
That is both uglier and slower and contains more characters to write.
That is however the better way of doing it because,
  • You have the same problem with single quote strings. The minute the string contains a single quote, it will break. e.g. $foo = 'Welcome to Steve's party'.
  • PHP variables are not parsed in single quote strings, aka you won't be able to do $foo = "Welcome to $location.";
anto91
Forum Commoner
Posts: 58
Joined: Mon Mar 10, 2008 10:59 am
Location: Sweden

Re: Write XML code in PHP

Post by anto91 »

Sekka wrote:
anto91 wrote:Other way of doing it

Code: Select all

 
<?php
echo "<xml tag=\"value\">sfoo</xml>";
?>
 
That is both uglier and slower and contains more characters to write.
That is however the better way of doing it because,
  • You have the same problem with single quote strings. The minute the string contains a single quote, it will break. e.g. $foo = 'Welcome to Steve's party'.
  • PHP variables are not parsed in single quote strings, aka you won't be able to do $foo = "Welcome to $location.";
For the sake of clarity i think its good that you cannot use variables in single quotes and i also think you shouldnt be able to in double its a bad habbit
User avatar
Sekka
Forum Commoner
Posts: 91
Joined: Mon Feb 18, 2008 10:25 am
Location: Huddersfield, West Yorkshire, UK

Re: Write XML code in PHP

Post by Sekka »

I'm not saying you should pass vars in that way, simply saying you can.
Post Reply