Results 1 to 3 of 3

Thread: Append to an XML File

  1. #1
    Join Date
    Aug 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Append to an XML File

    Hello,
    I am not very proficient with PHP although I I have been reading a ton of tutorials and forum posts over the last few days and working through getting the results I need. I have written a PHP script that creates an XML data file of 20 random numbers and a game number. I am currently able to use this XML file in my flash application.

    This script gets fired with in flash when an even ends. I am not sure how to go about adding another complete set of "Game", "gamemumber" and "number" nodes and text with in the root node. I am thinking that it would be best to read the XML file each time, append the nodes accordingly and then rewrite the XML file. Now, how big is too big of a file in order to do this? Eventually, there will be issues with the file size, right? The script gets fired about every 5 minutes or so.

    So, if allGames.xml does not exist, it gets created and starts the game number at 1 and dynamically adds the 20 numbers and nodes through an array. Then the script saves it to allGames.xml. When the script gets fired again, the file is there and the gamenumber increments to 2 and another <Game> data set is appended after </Game> but before </Container>.

    I have no idea as to where to start looking for this type of code or the best way to go about doing this.

    Any ideas or suggestions would be greatly appreciated.

    Code:
        <?xml version="1.0"?>
        <Container>
          <Game>
            <gamenumber>1</gamenumber>
            <number>49</number>
            <number>31</number>
            <number>79</number>
            <number>55</number>
            <number>19</number>
            <number>5</number>
            <number>20</number>
            <number>9</number>
            <number>25</number>
            <number>1</number>
            <number>53</number>
            <number>66</number>
            <number>2</number>
            <number>6</number>
            <number>42</number>
            <number>57</number>
            <number>15</number>
            <number>12</number>
            <number>13</number>
            <number>54</number>
          </Game>
        </Container>
    Code:
    <?php
    //Create the random numbers
    $balls = range(1,80);
    shuffle($balls);
    $pick = array_slice($balls,1,20);
    //$drawn = implode(", ",$pick);
    $arraySize = sizeof($pick);
    
    $xml_file= 'allGames.xml';
    
    $drawn = array();
    
    //Creates XML string and XML document using the DOM
     $dom = new DomDocument('1.0');
    
     //add root - <Container>
     $container = $dom->appendChild($dom->createElement('Container'));
    
     //add <Game> element to <Container>
     $game = $container->appendChild($dom->createElement('Game'));
    
    for ($i=0; $i!= $arraySize; $i++){
     
      $drawn [] = array(
      'number' => $pick[$i],
      );
      }  
    
     //add <gamenumber> element to <Game>
     $gamenumber = $game->appendChild($dom->createElement('gamenumber'));
     //add <gamenumner> text node element to <gamenumber>
     $gamenumber->appendChild(
                     $dom->createTextNode($gNumber+1));
     
     foreach( $drawn as $draw ) //Populates the number element with the random array
      {    
     
     //add <number> element to <Game>
     $number = $game->appendChild($dom->createElement('number'));
     //add <number> text node element to <number>
     $number->appendChild(
                     $dom->createTextNode($draw['number']));
     }
    
     //Generate the xml data files
     $dom->formatOutput = true; // set the formatOutput attribute of
                                // domDocument to true
    
    //Appends to $xml_file if it exists
     $handle = fopen($xml_file, 'a+');
            fwrite($handle, $dom->saveXML());
            fclose($handle);
                   
     echo $dom->saveXML();
     ?>

  2. #2
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    You can do this with regular expressions file_get_contents and file_put_contents. This treats the XML as text though if you want it treated as XML you'll probably have to do it another way.

    http://www.php.net/manual/en/book.pcre.php
    http://php.net/manual/en/function.file-get-contents.php
    http://www.php.net/manual/en/functio...t-contents.php

    What I mean by treated as XML

    http://www.php.net/manual/en/book.xml.php

    I've never used these so, there might be something the same there...
    Corrections to my coding/thoughts welcome.

  3. #3
    Join Date
    Aug 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks for the post. I will look at these. The text may not be a big deal, if I throw them into an array and then parse them out as xml. It does need to be well formed xml or Actionscript 3 in the flash application will error while trying to import it.

    I just need to understand how to append each data set after each </Game> node.

    I have studied the php manuals for the DOM and I am just not grasping the manipulation of the data. I understand how to read and write an xml file. I undertstand the getElementByTag but not seeing how to use that data and rewrite the tree with the new data at the end. I feel like I am close but I just can't see it.

Bookmarks

Posting Permissions

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