...

View Full Version : PHP 5 & XML



sd2
09-18-2005, 12:16 AM
Can someone please explain to me how I can create a XML file. I'd like to build a tree from scratch and populate it and save it to a file called markers.xml. So far I have following code



<?php

$doc = new DOMDocument('1.0');
$doc->formatOutput = true;

$root = $doc->createElement('markers');
$root = $doc->appendChild($root);

$child = $doc->createElement('marker');
$child = $root->appendChild($child);
$child->setAttribute("lat", "something");
$child->setAttribute("lon", "something");

echo $doc->saveXML() . "\n";

?>


which does exactly how I want it to be.

Now, how do I save it to a file? I tried $doc->save("test.xml"); but this didn't work...

The XML file needs to looks as follows:


<markers>
<marker lat="" lon=""/>
<marker lat="" lon=""/>
<marker lat="" lon=""/>
.
.
.
</markers>


Any help is greatly appreciated.

The Doc
09-18-2005, 12:50 AM
what about that:

file_put_contents('filename.xml',$doc->saveXML());

sd2
09-18-2005, 01:02 AM
and where can I find the file?

The Doc
09-18-2005, 01:16 AM
wait a second - where to find the file? I thought you want to create one?

read that:
http://www.php.net/manual/de/function.file-put-contents.php
and
http://www.php.net/manual/de/function.fwrite.php

sd2
09-18-2005, 04:06 AM
yes, i want to create a file. file_put_contents will create the filename.xml if non-existent. but where does it create it? i can't find it anywhere. maybe i should check my write-access rights.

Fou-Lu
09-18-2005, 06:00 AM
Yep, check your chmod. You shouldn't need to concern yourself with any filesystem functions (file_put_contents, fwrite, etc) while using the xml dom. I copied your code, paste it in with a save feature, dumped it no problem.

pjleonhardt
09-18-2005, 09:25 AM
It will create it in the same folder as the script that is creating it, unless you specify otherwise.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum