473,394 Members | 2,063 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How do I append data to XML file with PHP

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
0 2932

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

Similar topics

2
by: sm | last post by:
How to "Insert" (not append) new text segment to an existing text file? Assume that we have text file as shown below; Elvis Sofia Kylix BCB--> How to insert here? Atten BuilderX Roma
3
by: Jonathan Buckland | last post by:
Can someone give me an example how to append data without having to load the complete XML file. Is this possible? Jonathan
2
by: Matrix | last post by:
Greetings I want to append records in a XML file,I use XMLTextWriter,but i found it only create a XML file,not append records in exist XML file.why?...
3
by: Mohan | last post by:
Hi I face a problem with the stl string append on our 32-bit SLES9 machine . (It is not happening on SLES9 64-bit machines and on SuSE 10). When we read a 179MB file into the memory,...
4
by: MN | last post by:
I have to import a tab-delimited text file daily into Access through a macro. All of the data needs to be added to an existing table. Some of the data already exists but may be updated by the...
3
by: wvmbark | last post by:
First time poster... I just found this forum and it appears there's plenty of people here that could make short work of problem that's been driving me absolutely bonkers for months. Every day we...
2
by: Gaby Sandoval | last post by:
I have this code. The user can read teh record from the text file just fine. They can click the NEXT button and it reads the next record (which is just the next line from the text file). If the...
3
by: Freddy Coal | last post by:
Hi, I would like append strings to a binary file, but I donīt understand how make that. I try with: FileOpen(1, Folder_Trabajo & "\Toma_Trazas.FC", OpenMode.Append, OpenAccess.Write,...
1
by: wish | last post by:
Hello, I have problem is when i append new data in text file always append at the last of the text file. Am i can append the new data in beginning of the text file? Mean when i have update data...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.