Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7

Thread: PHP 5 & XML

  1. #1
    sd2
    sd2 is offline
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP 5 & XML

    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 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:
    Code:
    <markers>
       <marker lat="" lon=""/>
       <marker lat="" lon=""/>
       <marker lat="" lon=""/>
       .
       .
       .
    </markers>
    Any help is greatly appreciated.

  • #2
    New Coder
    Join Date
    Sep 2005
    Location
    Berlin, Germany
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    what about that:
    PHP Code:
    file_put_contents('filename.xml',$doc->saveXML()); 

  • #3
    sd2
    sd2 is offline
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    and where can I find the file?

  • #4
    New Coder
    Join Date
    Sep 2005
    Location
    Berlin, Germany
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    wait a second - where to find the file? I thought you want to create one?

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

  • #5
    sd2
    sd2 is offline
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #7
    New Coder
    Join Date
    Apr 2005
    Posts
    97
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It will create it in the same folder as the script that is creating it, unless you specify otherwise.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •