Results 1 to 8 of 8

Thread: Writing to XML files with PHP

  1. #1
    foodpk's Avatar
    1,335
    posts
    ask me about being a jerk

    Writing to XML files with PHP

    XML is really cool, I'm amazed at how easy it is to use and what it can achieve. But now I'm wondering about what the easiest method of adding data into an XML file via PHP is. I'm somewhat proficient with PHP (and MySQL), just not too much. For instance we have an XML file like so.
    Code:
    <news>
    	<item>
    		<heading>This is the heading!</heading>
    		<content>Content! Blah blah blah blah blah blah blah!</content>
    	</item>
    	<item>
    		<heading>This is the heading!</heading>
    		<content>Content! Blah blah blah blah blah blah blah!</content>
    	</item>
    	<item>
    		<heading>This is the heading!</heading>
    		<content>Content! Blah blah blah blah blah blah blah!</content>
    	</item>
    </news>
    So how would we go about adding item elements (of course containing headings and content) into our XML file via PHP? Are there any good libraries that do that or built in functions?

  2. #2
    foodpk's Avatar
    1,335
    posts
    ask me about being a jerk
    *gentle bump, I hate bumping, but it must be done*

  3. #3
    icio's Avatar
    3,810
    posts
    looks better in lowercase
    try posting it into the correct forum:
    http://www.kirupa.com/forum/forumdisplay.php?f=11

    there is also a tutorial on this somewhere on kirupa.com

    but - to get things into the XML file that is loaded, you echo them.
    PHP Code:
    echo ("<xml><item>item1</item><item>item2</item></xml>"); 
    and you would use loops and variables to put in what you want
    "60% of the time it works... every time." -- Paul Rudd as Brian Fantana.

  4. #4
    foodpk's Avatar
    1,335
    posts
    ask me about being a jerk
    Hmmm, I'm not sure I follow, you just echo them and they write into the xml file?
    Oh, by the way, I didn't even see that forum existed, I apologize. It would be cool if one of the mods could move this thread there so I don't have to make another one.

  5. #5
    Quote Originally Posted by foodpk
    ...... It would be cool if one of the mods could move this thread there so I don't have to make another one.
    Done

    scotty

  6. #6
    PHP5 has fantastic XML support. Check out simplexml: http://us3.php.net/manual/en/ref.simplexml.php

    What cyberathlete says works as well and is a nice and easy way to do things for simple xml files.

    There are a variety of methods of dealing with XML in PHP4, I suggest starting by looking at the expat, domxml, etc... sections of php.net if you really want to get into it.

  7. #7
    icio's Avatar
    3,810
    posts
    looks better in lowercase
    well, no - it doesn't write to the xml file - the php is the xml file.
    for example, if we were loading an xml file into flash - we wouldn't load an xml file, we would load the php file.

    the echo function writes to the file that is then sent to the user when the download it via http.
    for example, if i goto http://www.website.com/phpFile.php and in that file is:
    PHP Code:
    echo("this is being generated by the server"); 
    the user would only see, "this is being generated by the server" minus the quotation marks. i don't know how clear i'm making myself.

    if you were wanting to load this into flash, you'd load xml.php - which then echoes the information that is required, in the correct order.

    :S
    good thing to do is check out the tutorial on kirupa.com main page
    "60% of the time it works... every time." -- Paul Rudd as Brian Fantana.

  8. #8
    Or if you really need a proper XML file rather than a PHP page generating XML, you can just write all of the XML in a variable, and then get PHP to generate the actual XML file like this:

    PHP Code:
    $xmlContent '<xml><content /></xml>';
    $filename 'xmlFile.xml';
    $fp fopen($filename"w");
    $write fputs($fp$xmlContent);
    if(!
    fclose($fp)) {
    echo 
    "Error creating file";
    } else {
    echo 
    "File Created";

    Description of fopen
    Description of fputs
    And a description of fclose

    Hope that helps

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012