473,581 Members | 2,814 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I append data to XML file with PHP

1 New Member
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);
  5. if ($num != 0) {
  7.  $file= fopen("data/frlpsData.xml", "w");
  9.  $_xml ="<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n";
  11.  $_xml .="<statutes>\r\n";
  12.  $_xml .="<states>\r\n";
  14.  while ($row = mysql_fetch_array($result)) {
  16.     $_xml .="\t<state id=\"". $row["stateID"] . "\" name=\"". $row["stateName"] . "\">\r\n";
  18.  if ($row["id"]) {
  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";
  48.   } else {
  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";
  78.  }
  79.     $_xml .="\t</state>\r\n"; 
  80.  }
  81.  $_xml .="</states>\r\n";
  82.  $_xml .="</statutes>";
  84.  fwrite($file, $_xml);
  86.  fclose($file);
  88.  echo "XML has been written.  <a href=\"data/frlpsData.xml\">View the XML.</a>";
  90.  } else {
  92.  echo "No Records found";
  94.  } ?>
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">
  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>                                
  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>                                
  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>
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 2947

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

Similar topics

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
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
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? ------------------------------------------------- XmlTextWriter myXmlTextWrite = new XmlTextWriter(@"C:\myconfig.xml"); myXmlTextWrite.WriteStartDocument();...
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, m_oString.append(awchDestinationStr, a_nTotalNumOfUnicodes) is crashing Leads to a crash in libstdc++ new() on 32-bit SLES9 machine byut working fine...
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 imported text file. I can update the data through an update query or append the entire import table through an append query. Is there a way to combine...
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 incur numerous service problems “Events”. Each morning we have a global conference call where events which occurred within the previous 24 hours are...
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 user makes changes to a record using the application they have to option to click the APPEND button to overwrite there changes on the file. The...
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, OpenShare.Default) FilePut(1, "My string") '<****** ERROR IN THIS LINE ****** FileClose(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 the text file and always write at the beginning of the text file and the previous data at the below of the new data.. Can anyone have idea about it?...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.