Appending to XML File

Hello,

I have posted this on another forum with no response. I am usually able to figure things out in time but this one has me stumped. I am fairly new to PHP but have poked around with it from time to time for the last few years. I can usually find a tutorial or a an example and make it work for what I need. That is how I have gotten to this stage of my project.

I have a script that creates an XML based on 1 game number and 20 random numbers. The script will successfully create the XML tree each time it is ran. The problem is when it runs it appends the new tree at the end and my flash errors with it not being well structured.

I am wondering if the lack of response to my problem is that a lot of coders are not versed in the DOM. It is complicated to me, although I now understand how to read in an XML file and Write and XML file. I just can no grasp how to edit and append data to an XML file.

The way my script works is it gets fired from an event in my flash application every 5 minutes. If the file is not there then it gets created and the game number is 1 and randomly generates 20 numbers. Then in about 5 minutes it fires off again. Because there is data in in the file the new data must be appended inside the root node but after the /Game node. This has to happen for each new set of data.

I am thinking that that best way to tackle this is to read the entire XML tree structure in each time and then add the new data and rewrite the file. How big is too big to work with XML trees like this? How much memory can I expect to use on the larger files. Will it cause a problem for system resources. And finally is there possibly a better way to achieve the same results?

All of the tutorials I have seen work with reading int the data and then either deleting a the data or the node or using that values in variables, not with reading the data, assigning it to the proper nodes and then adding new nodes after that. There is no real manipulation of the data just appending new data after a specific set.

If I were to use arrays, would the node names be included in the array values when read in?? This seems so simple in mind but a little complex to execute.

Any help or suggestions would greatly be appreciated. If you know of a tutorial where this scenario applies, I would like to look at it.

Thanks. I look forward to some type of reply.

Here is my XML file

<?xml version="1.0"?>
<Container>
  <Game>
    <gamenumber>1</gamenumber>
    <number>10</number>
    <number>11</number>
    <number>12</number>
    <number>13</number>
    <number>14</number>
    <number>15</number>
    <number>16</number>
    <number>17</number>
    <number>18</number>
    <number>19</number>
    <number>20</number>
    <number>21</number>
    <number>22</number>
    <number>23</number>
    <number>24</number>
    <number>25</number>
    <number>26</number>
    <number>27</number>
    <number>28</number>
    <number>29</number>
  </Game>
  <Game>
    <gamenumber>2</gamenumber>
    <number>30</number>
    <number>31</number>
    <number>32</number>
    <number>33</number>
    <number>34</number>
    <number>35</number>
    <number>36</number>
    <number>37</number>
    <number>38</number>
    <number>39</number>
    <number>40</number>
    <number>41</number>
    <number>42</number>
    <number>43</number>
    <number>44</number>
    <number>45</number>
    <number>46</number>
    <number>47</number>
    <number>48</number>
    <number>49</number>
  </Game>
**** EACH NEW DATA SET MUST BE APPENDED HERE WITH THE <Game><gamenumber>and <number> ****
</Container>

And the PHP Script:
[php]

<?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 = $dom->appendChild($dom->createElement('Container')); //add element to $game = $container->appendChild($dom->createElement('Game')); for ($i=0; $i!= $arraySize; $i++){ $drawn [] = array( 'number' => $pick[$i], ); } //add element to $gamenumber = $game->appendChild($dom->createElement('gamenumber')); //add text node element to $gamenumber->appendChild( $dom->createTextNode($gNumber+1)); foreach( $drawn as $draw ) //Populates the number element with the random array { //add element to $number = $game->appendChild($dom->createElement('number')); //add text node element to $number->appendChild( $dom->createTextNode($draw['number'])); } //Generate the xml data files $dom->formatOutput = true; // set the formatOutput attribute of // domDocument to true // save XML as two files //Appends to $xml_file if it exists $handle = fopen($xml_file, 'a+'); fwrite($handle, $dom->saveXML()); fclose($handle); //Overwrites gameNumbers.xml $handle = fopen('gameNumbers.xml', 'w'); fwrite($handle, $dom->saveXML()); fclose($handle); echo $dom->saveXML(); ?>

[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service