473,748 Members | 6,161 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);
  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 2960

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

Similar topics

2
2816
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
23956
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
12908
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(); myXmlTextWrite.WriteStartElement("Record"); myXmlTextWrite.WriteStartElement("Topic"); myXmlTextWrite.WriteString(XMLTopic);
3
2099
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 with SLES9 64-bit machines and on SuSE 10
4
7388
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 the two so that I can update existing records and append only new records (without duplicating...
3
8075
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 discussed. Prior to the call, an analyst has to review these events and provide a report, ‘The Morning...
2
2562
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 file has a line of text for each record....... Here is the code for when they click the APPEND...
3
4821
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)
1
4991
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? Thanks in advance...
0
8989
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9537
Oralloy
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9367
jinu1996
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9243
tracyyun
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4599
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3309
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
2
2780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2213
bsmnconsultancy
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.