PDA

View Full Version : PHP variables added to XML



Coda
27-08-2008, 05:28 PM
Basically i have a XML file which i read from for e.g.


<person>
<firstName> tom</firstName>
</person>now is it possible to make the first name data read from a php variable say for e.g.


$firstname = "tom";
<person>
<firstName> tom</firstName>
// so variable <firstName> is always saved as childNodes [i][0]
</person>Is this possible :S?

Decode
27-08-2008, 05:45 PM
<?php
$file = file_get_contents( "xmlfile.xml" );
$file = explode( "<firstName>" , $file);
echo $file[0];
?>

Coda
27-08-2008, 07:44 PM
yeah i know how to retrieve data from a XML file, but what i want is to write data into the XML file but storing it as a php variable rather than a XML variable.

Like say i have in my php page a variable named $name i want to be able to put whatever is stored in that PHP variable into the XML file.

MrCraig
27-08-2008, 07:51 PM
fwrite?

Coda
27-08-2008, 08:29 PM
yeah but how would i be able to write in a specific place in the XML file without deleting what already in there?

Protege
28-08-2008, 12:08 PM
find 'tom', replace it with 'god'...

Dentafrice
28-08-2008, 08:35 PM
Do this..


<?php

$name = "Caleb";

$xml .= "<blah>";
$xml .= "<name>" . $name . "</name>";
$xml .= "</blah>";

header("Content-type: text/xml");

echo $xml;

?>

Then just use mod_rewrite to rewrite that to blah.xml or something.

Protege
28-08-2008, 10:13 PM
<?php

$exampleXML = ' <persons>
<dude>James</dude>
<dude>Celeb</dude>
<dude>Mark</dude>
<dude>God</dude>
</persons>';

$xmlExplode = explode( '<persons>', $exampleXML );
$xmlPersons = str_replace( '</persons>', '', $xmlExplode[ 1 ] );

$xmlExplode = explode( '<dude>', $xmlPersons );


$xmlCeleb = str_replace( '</dude>', '', $xmlExplode[ 2 ] );
$xmlJames = str_replace( '</dude>', '', $xmlExplode[ 1 ] );

echo( $xmlCeleb );

?>


Something maybe around that?

Dentafrice
28-08-2008, 11:22 PM
Why not just use SimpleXML? :P

Source
29-08-2008, 11:01 AM
I was gonna recommend SimpleXML aswel.

Coda
29-08-2008, 04:23 PM
Thanks worked :)

Want to hide these adverts? Register an account for free!