By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,857 Members | 1,768 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,857 IT Pros & Developers. It's quick & easy.

How do I append data to XML file with PHP

P: 1
I have a XML file I am working with. This file has been created by hand and I now need to develop a PHP script that will create it in the same format.

Here is what I have thus far:

Expand|Select|Wrap|Line Numbers
  1. $query = "select * from " . $table_name . " ORDER BY stateID ASC";
  2. $result = mysql_query($query, $connection) or die("Could not complete database query");
  3. $num = mysql_num_rows($result);
  4.  
  5. if ($num != 0) {
  6.  
  7.  $file= fopen("data/frlpsData.xml", "w");
  8.  
  9.  $_xml ="<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n";
  10.  
  11.  $_xml .="<statutes>\r\n";
  12.  $_xml .="<states>\r\n";
  13.  
  14.  while ($row = mysql_fetch_array($result)) {
  15.  
  16.     $_xml .="\t<state id=\"". $row["stateID"] . "\" name=\"". $row["stateName"] . "\">\r\n";
  17.  
  18.  if ($row["id"]) {
  19.  
  20.     $_xml .="\t\t<counties>\r\n";
  21.     $_xml .="\t\t\t<county name=\"". $row["countyName"] . "\">\r\n";
  22.     $_xml .="\t\t\t\t<documents>\r\n";
  23.     $_xml .="\t\t\t\t\t<document name=\"". $row["docName"] . "\">\r\n";
  24.     $_xml .="\t\t\t\t\t\t<types>\r\n";
  25.     $_xml .="\t\t\t\t\t\t\t<type id=\"". $row["entityType"] . "\" name=\"". $row["entityName"] . "\">\r\n";
  26.     $_xml .="\t\t\t\t\t\t\t\t<line>" . $row["line1"] . "</line>\r\n";
  27.     $_xml .="\t\t\t\t\t\t\t\t<line></line>\r\n";
  28.     $_xml .="\t\t\t\t\t\t\t\t<line>" . $row["line2"] . "</line>\r\n";
  29.     $_xml .="\t\t\t\t\t\t\t\t<line></line>\r\n";
  30.     $_xml .="\t\t\t\t\t\t\t\t<line>" . $row["line3"] . "</line>\r\n";
  31.     $_xml .="\t\t\t\t\t\t\t\t<line></line>\r\n";
  32.     $_xml .="\t\t\t\t\t\t\t\t<line>" . $row["line4"] . "</line>\r\n";
  33.     $_xml .="\t\t\t\t\t\t\t\t<line></line>\r\n";
  34.     $_xml .="\t\t\t\t\t\t\t\t<line>" . $row["line5"] . "</line>\r\n";
  35.     $_xml .="\t\t\t\t\t\t\t\t<line></line>\r\n";
  36.     $_xml .="\t\t\t\t\t\t\t\t<line>" . $row["line6"] . "</line>\r\n";
  37.     $_xml .="\t\t\t\t\t\t\t\t<line></line>\r\n";
  38.     $_xml .="\t\t\t\t\t\t\t\t<line>" . $row["line7"] . "</line>\r\n";
  39.     $_xml .="\t\t\t\t\t\t\t\t<line></line>\r\n";
  40.     $_xml .="\t\t\t\t\t\t\t\t<line>" . $row["line8"] . "</line>\r\n";
  41.     $_xml .="\t\t\t\t\t\t\t</type>\r\n";
  42.     $_xml .="\t\t\t\t\t\t</types>\r\n";
  43.     $_xml .="\t\t\t\t\t</document>\r\n";
  44.     $_xml .="\t\t\t\t</documents>\r\n";
  45.     $_xml .="\t\t\t</county>\r\n";
  46.     $_xml .="\t\t</counties>\r\n";
  47.  
  48.   } else {
  49.  
  50.     $_xml .="\t\t<counties>\r\n";
  51.     $_xml .="\t\t\t<county name=\"". $row["countyName"] . "\">\r\n";
  52.     $_xml .="\t\t\t\t<documents>\r\n";
  53.     $_xml .="\t\t\t\t\t<document name=\"". $row["docName"] . "\">\r\n";
  54.     $_xml .="\t\t\t\t\t\t<types>\r\n";
  55.     $_xml .="\t\t\t\t\t\t\t<type id=\"". $row["entityType"] . "\" name=\"". $row["entityName"] . "\">\r\n";
  56.     $_xml .="\t\t\t\t\t\t\t\t<line>No Data For This State. Please Update.</line>\r\n";
  57.     $_xml .="\t\t\t\t\t\t\t\t<line></line>\r\n";
  58.     $_xml .="\t\t\t\t\t\t\t\t<line></line>\r\n";
  59.     $_xml .="\t\t\t\t\t\t\t\t<line></line>\r\n";
  60.     $_xml .="\t\t\t\t\t\t\t\t<line></line>\r\n";
  61.     $_xml .="\t\t\t\t\t\t\t\t<line></line>\r\n";
  62.     $_xml .="\t\t\t\t\t\t\t\t<line></line>\r\n";
  63.     $_xml .="\t\t\t\t\t\t\t\t<line></line>\r\n";
  64.     $_xml .="\t\t\t\t\t\t\t\t<line></line>\r\n";
  65.     $_xml .="\t\t\t\t\t\t\t\t<line></line>\r\n";
  66.     $_xml .="\t\t\t\t\t\t\t\t<line></line>\r\n";
  67.     $_xml .="\t\t\t\t\t\t\t\t<line></line>\r\n";
  68.     $_xml .="\t\t\t\t\t\t\t\t<line></line>\r\n";
  69.     $_xml .="\t\t\t\t\t\t\t\t<line></line>\r\n";
  70.     $_xml .="\t\t\t\t\t\t\t\t<line></line>\r\n";
  71.     $_xml .="\t\t\t\t\t\t\t</type>\r\n";
  72.     $_xml .="\t\t\t\t\t\t</types>\r\n";
  73.     $_xml .="\t\t\t\t\t</document>\r\n";
  74.     $_xml .="\t\t\t\t</documents>\r\n";
  75.     $_xml .="\t\t\t</county>\r\n";
  76.     $_xml .="\t\t</counties>\r\n";
  77.  
  78.  }
  79.     $_xml .="\t</state>\r\n"; 
  80.  }
  81.  $_xml .="</states>\r\n";
  82.  $_xml .="</statutes>";
  83.  
  84.  fwrite($file, $_xml);
  85.  
  86.  fclose($file);
  87.  
  88.  echo "XML has been written.  <a href=\"data/frlpsData.xml\">View the XML.</a>";
  89.  
  90.  } else {
  91.  
  92.  echo "No Records found";
  93.  
  94.  } ?>
  95.  
Now, my problem is this. I need to create the XML file with the following format:

Expand|Select|Wrap|Line Numbers
  1. <states>
  2.         <state id="AL" name="Alabama">
  3.  
  4.                 <county name="Default" fipsID="9999">
  5.                     <documents>
  6.                         <document name="Summons and Complaint">
  7.                             <types>
  8.                                 <type id="biz" name="Business">
  9.                                     <line>ANY AUTHORIZED AGENT OVER THE AGE OF 18</line>
  10.                                     <line></line>
  11.                                     <line>DESCRIPTION: Sex____ Race_____ Age_____ Height_____ Weight_____ Hair_____ Glasses Y/N</line>                                                            
  12.                                 </type>
  13.                                 <type id="ind" name="Individual">
  14.                                     <line>PERSONAL-INDIVIDUAL SERVICE</line>
  15.                                     <line></line>
  16.                                     <line>SUBSTITUTE SERVICE -  ANY PERSON OVER THE AGE OF 18 WHO RESIDES AT DEFENDANT'S PLACE OF ABODE.</line>
  17.                                     <line></line>
  18.                                     <line>DESCRIPTION: Sex____ Race_____ Age_____ Height_____ Weight_____ Hair_____ Glasses Y/N</line>                                
  19.                                 </type>
  20.                             </types>
  21.                         </document>
  22.                         <document name="Subpeona">
  23.                             <types>
  24.                                 <type id="biz" name="Business">
  25.                                     <line>ANY AUTHORIZED AGENT OVER THE AGE OF 18</line>
  26.                                     <line></line>
  27.                                     <line>DESCRIPTION: Sex____ Race_____ Age_____ Height_____ Weight_____ Hair_____ Glasses Y/N</line>
  28.                                     <line>    </line>                                
  29.  
  30.                                 </type>
  31.                                 <type id="ind" name="Individual">
  32.                                     <line>MUST MAKE 3 ATTEMPTS ON 3 SEPERATE DAYS BEFORE SUBSERVING</line>
  33.                                     <line></line>
  34.                                     <line>ANY PERSON OVER THE AGE OF 18 WHO RESIDES AT SERVEES ABODE</line>
  35.                                     <line></line>
  36.                                     <line>DESCRIPTION: Sex____ Race_____ Age_____ Height_____ Weight_____ Hair_____ Glasses Y/N    </line>                                
  37.                                 </type>
  38.                             </types>
  39.                         </document>
  40.                         <document name="Subpoena Duces Tecum">
  41.                             <types>
  42.                                 <type id="biz" name="Business">
  43.                                     <line>ANY AUTHORIZED AGENT OVER THE AGE OF 18</line>
  44.                                     <line></line>
  45.                                     <line>DESCRIPTION: Sex____ Race_____ Age_____ Height_____ Weight_____ Hair_____ Glasses Y/N    </line>
  46.                                     <line>    </line>                                
  47.  
  48.                                 </type>
  49.                                 <type id="ind" name="Individual">
  50.                                     <line>MUST MAKE 3 ATTEMPTS ON 3 SEPERATE DAYS BEFORE SUBSERVING</line>
  51.                                     <line></line>
  52.                                     <line>ANY PERSON OVER THE AGE OF 18 WHO RESIDES AT SERVEES ABODE</line>
  53.                                     <line></line>
  54.                                     <line>DESCRIPTION: Sex____ Race_____ Age_____ Height_____ Weight_____ Hair_____ Glasses Y/N    </line>                                
  55.                                 </type>
  56.                             </types>
  57.                         </document>
  58.                     </documents>
  59.                 </county>        
  60.         </state>
  61.           </states>
  62.  
This is a truncated example, of course, leaving out the data for the other states in the country for this example.

What my PHP code does is creates a XML file from a MySQL table, but instead of appending the <document/> data to the various states/county, it creates a separate record for each document for each state. I need to condense the data so that all the documents are listed under the appropriate state for the appropriate county. I have been racking my brain trying to figure this out and I have come to the conclusion that I have just enough knowledge of PHP/MySQL/XML to make me dangerous.

What I am after is an example, some direction, or perhaps a tutorial that will show me how this is done and hopefully I can figure it out from there. All of the examples I have been able to find show how to append a new record to the file but I haven't been able to find one that shows how to append a child of a record while iterating through the results of the query.

Any help would be greatly appreciated.
Oct 30 '09 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.