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:
-
$query = "select * from " . $table_name . " ORDER BY stateID ASC";
-
$result = mysql_query($query, $connection) or die("Could not complete database query");
-
$num = mysql_num_rows($result);
-
-
if ($num != 0) {
-
-
$file= fopen("data/frlpsData.xml", "w");
-
-
$_xml ="<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n";
-
-
$_xml .="<statutes>\r\n";
-
$_xml .="<states>\r\n";
-
-
while ($row = mysql_fetch_array($result)) {
-
-
$_xml .="\t<state id=\"". $row["stateID"] . "\" name=\"". $row["stateName"] . "\">\r\n";
-
-
if ($row["id"]) {
-
-
$_xml .="\t\t<counties>\r\n";
-
$_xml .="\t\t\t<county name=\"". $row["countyName"] . "\">\r\n";
-
$_xml .="\t\t\t\t<documents>\r\n";
-
$_xml .="\t\t\t\t\t<document name=\"". $row["docName"] . "\">\r\n";
-
$_xml .="\t\t\t\t\t\t<types>\r\n";
-
$_xml .="\t\t\t\t\t\t\t<type id=\"". $row["entityType"] . "\" name=\"". $row["entityName"] . "\">\r\n";
-
$_xml .="\t\t\t\t\t\t\t\t<line>" . $row["line1"] . "</line>\r\n";
-
$_xml .="\t\t\t\t\t\t\t\t<line></line>\r\n";
-
$_xml .="\t\t\t\t\t\t\t\t<line>" . $row["line2"] . "</line>\r\n";
-
$_xml .="\t\t\t\t\t\t\t\t<line></line>\r\n";
-
$_xml .="\t\t\t\t\t\t\t\t<line>" . $row["line3"] . "</line>\r\n";
-
$_xml .="\t\t\t\t\t\t\t\t<line></line>\r\n";
-
$_xml .="\t\t\t\t\t\t\t\t<line>" . $row["line4"] . "</line>\r\n";
-
$_xml .="\t\t\t\t\t\t\t\t<line></line>\r\n";
-
$_xml .="\t\t\t\t\t\t\t\t<line>" . $row["line5"] . "</line>\r\n";
-
$_xml .="\t\t\t\t\t\t\t\t<line></line>\r\n";
-
$_xml .="\t\t\t\t\t\t\t\t<line>" . $row["line6"] . "</line>\r\n";
-
$_xml .="\t\t\t\t\t\t\t\t<line></line>\r\n";
-
$_xml .="\t\t\t\t\t\t\t\t<line>" . $row["line7"] . "</line>\r\n";
-
$_xml .="\t\t\t\t\t\t\t\t<line></line>\r\n";
-
$_xml .="\t\t\t\t\t\t\t\t<line>" . $row["line8"] . "</line>\r\n";
-
$_xml .="\t\t\t\t\t\t\t</type>\r\n";
-
$_xml .="\t\t\t\t\t\t</types>\r\n";
-
$_xml .="\t\t\t\t\t</document>\r\n";
-
$_xml .="\t\t\t\t</documents>\r\n";
-
$_xml .="\t\t\t</county>\r\n";
-
$_xml .="\t\t</counties>\r\n";
-
-
} else {
-
-
$_xml .="\t\t<counties>\r\n";
-
$_xml .="\t\t\t<county name=\"". $row["countyName"] . "\">\r\n";
-
$_xml .="\t\t\t\t<documents>\r\n";
-
$_xml .="\t\t\t\t\t<document name=\"". $row["docName"] . "\">\r\n";
-
$_xml .="\t\t\t\t\t\t<types>\r\n";
-
$_xml .="\t\t\t\t\t\t\t<type id=\"". $row["entityType"] . "\" name=\"". $row["entityName"] . "\">\r\n";
-
$_xml .="\t\t\t\t\t\t\t\t<line>No Data For This State. Please Update.</line>\r\n";
-
$_xml .="\t\t\t\t\t\t\t\t<line></line>\r\n";
-
$_xml .="\t\t\t\t\t\t\t\t<line></line>\r\n";
-
$_xml .="\t\t\t\t\t\t\t\t<line></line>\r\n";
-
$_xml .="\t\t\t\t\t\t\t\t<line></line>\r\n";
-
$_xml .="\t\t\t\t\t\t\t\t<line></line>\r\n";
-
$_xml .="\t\t\t\t\t\t\t\t<line></line>\r\n";
-
$_xml .="\t\t\t\t\t\t\t\t<line></line>\r\n";
-
$_xml .="\t\t\t\t\t\t\t\t<line></line>\r\n";
-
$_xml .="\t\t\t\t\t\t\t\t<line></line>\r\n";
-
$_xml .="\t\t\t\t\t\t\t\t<line></line>\r\n";
-
$_xml .="\t\t\t\t\t\t\t\t<line></line>\r\n";
-
$_xml .="\t\t\t\t\t\t\t\t<line></line>\r\n";
-
$_xml .="\t\t\t\t\t\t\t\t<line></line>\r\n";
-
$_xml .="\t\t\t\t\t\t\t\t<line></line>\r\n";
-
$_xml .="\t\t\t\t\t\t\t</type>\r\n";
-
$_xml .="\t\t\t\t\t\t</types>\r\n";
-
$_xml .="\t\t\t\t\t</document>\r\n";
-
$_xml .="\t\t\t\t</documents>\r\n";
-
$_xml .="\t\t\t</county>\r\n";
-
$_xml .="\t\t</counties>\r\n";
-
-
}
-
$_xml .="\t</state>\r\n";
-
}
-
$_xml .="</states>\r\n";
-
$_xml .="</statutes>";
-
-
fwrite($file, $_xml);
-
-
fclose($file);
-
-
echo "XML has been written. <a href=\"data/frlpsData.xml\">View the XML.</a>";
-
-
} else {
-
-
echo "No Records found";
-
-
} ?>
-
Now, my problem is this. I need to create the XML file with the following format:
-
<states>
-
<state id="AL" name="Alabama">
-
-
<county name="Default" fipsID="9999">
-
<documents>
-
<document name="Summons and Complaint">
-
<types>
-
<type id="biz" name="Business">
-
<line>ANY AUTHORIZED AGENT OVER THE AGE OF 18</line>
-
<line></line>
-
<line>DESCRIPTION: Sex____ Race_____ Age_____ Height_____ Weight_____ Hair_____ Glasses Y/N</line>
-
</type>
-
<type id="ind" name="Individual">
-
<line>PERSONAL-INDIVIDUAL SERVICE</line>
-
<line></line>
-
<line>SUBSTITUTE SERVICE - ANY PERSON OVER THE AGE OF 18 WHO RESIDES AT DEFENDANT'S PLACE OF ABODE.</line>
-
<line></line>
-
<line>DESCRIPTION: Sex____ Race_____ Age_____ Height_____ Weight_____ Hair_____ Glasses Y/N</line>
-
</type>
-
</types>
-
</document>
-
<document name="Subpeona">
-
<types>
-
<type id="biz" name="Business">
-
<line>ANY AUTHORIZED AGENT OVER THE AGE OF 18</line>
-
<line></line>
-
<line>DESCRIPTION: Sex____ Race_____ Age_____ Height_____ Weight_____ Hair_____ Glasses Y/N</line>
-
<line> </line>
-
-
</type>
-
<type id="ind" name="Individual">
-
<line>MUST MAKE 3 ATTEMPTS ON 3 SEPERATE DAYS BEFORE SUBSERVING</line>
-
<line></line>
-
<line>ANY PERSON OVER THE AGE OF 18 WHO RESIDES AT SERVEES ABODE</line>
-
<line></line>
-
<line>DESCRIPTION: Sex____ Race_____ Age_____ Height_____ Weight_____ Hair_____ Glasses Y/N </line>
-
</type>
-
</types>
-
</document>
-
<document name="Subpoena Duces Tecum">
-
<types>
-
<type id="biz" name="Business">
-
<line>ANY AUTHORIZED AGENT OVER THE AGE OF 18</line>
-
<line></line>
-
<line>DESCRIPTION: Sex____ Race_____ Age_____ Height_____ Weight_____ Hair_____ Glasses Y/N </line>
-
<line> </line>
-
-
</type>
-
<type id="ind" name="Individual">
-
<line>MUST MAKE 3 ATTEMPTS ON 3 SEPERATE DAYS BEFORE SUBSERVING</line>
-
<line></line>
-
<line>ANY PERSON OVER THE AGE OF 18 WHO RESIDES AT SERVEES ABODE</line>
-
<line></line>
-
<line>DESCRIPTION: Sex____ Race_____ Age_____ Height_____ Weight_____ Hair_____ Glasses Y/N </line>
-
</type>
-
</types>
-
</document>
-
</documents>
-
</county>
-
</state>
-
</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.