|
|
How To Format XML Pad Files
The following is sample code in PHP for formatting ASP pad files in XML format:
<?PHP
/*
Program: xmlpadfile.php
Author: William H. Bradshaw
Web Site: http://www.zappersoftware.com
Date Written: 2002/12/20
Objective: To parse an XML format PAD file
See http://www.asp-shareware.org/pad/ for more information
Status: Example Code Only. If you use this code then please
add a link on your site back to my site http://www.zappersoftware.com/
NOTES: Could set $file field to http://www.website.com/nameofpad.xml after the
user submits the URL of their padfile. Also see URLENCODE.
Check to ensure that your PHP config allows for file sockets or this will
not work for http protocol schemes. If your ISP does not allow for file
sockets then you could add a file upload step prior to this routine.
WARNING: Do not trust ANY external files.
You should also add htmlspecialchars($fieldname) check to all untrusted
files. You will have to apply this check to each field otherwise your XML
will not work to prevent any malicious users from uploading damaging PHP code.
IT COULD delete all of your files on your server or who knows???
*/
// Define all of your fields here that you wish to add to your database.
$program = "";
$version = "";
$company = "";
$desc_250 = "";
$file = "ZSSHGD10PAD.xml";
$depth = array();
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterHandler");
if (!($fp = fopen($file, "r"))) {
  die("could not open XML input");
}
echo "*** XML ASP Pad File Listing ***";
while ($data = fread($fp, 4096)) {
   if (!xml_parse($xml_parser, $data, feof($fp))) {
     die(sprintf("XML error: %s at line %d",
   xml_error_string(xml_get_error_code($xml_parser)),
   xml_get_current_line_number($xml_parser)));
   }
 }
xml_parser_free($xml_parser);
// Add your MySQL statement here to insert all of your fields
echo "$program";
echo "$version";
echo "$company";
echo "$desc_250";
echo "*** Done ***";
function startElement($parser, $name, $attrs) {
global $depth;
global $cdata;
$cdata = array();
for ($i = 0; $i < $depth[$parser]; $i++) {
   print " ";
  }
  $depth[$parser]++;
}
function endElement($parser, $name) {
global $depth;
global $data;
global $cdata;
global $line;
// Define all of your MySQL global variables here
global $program;
global $version;
global $company;
global $desc_250;
$data[$name] = trim( implode('', $cdata) );
$depth[$parser]--;
// FOR DEBUGGING
// echo "XML Field:" . "$name"; // Name of XML field
// echo "XML Data:" . "$cdata[0]"; // Contents of XML data
switch ($name) {
  case 'PROGRAM_NAME':
   $program = $cdata[0];
   break;
  case 'PROGRAM_NAME':
   $version = $cdata[0];
   break;
  case 'COMPANY_NAME':
   $company = $cdata[0];
   break;
  case 'CHAR_DESC_250':
// This is just a quick version.
// You need a loop here to grab the entire array.
// I am just concatenating this as an example.
   $desc_250 = $cdata[0] . $cdata[1] . $cdata[2] . $cdata[3];
   break;
// Add all of your other fields here in this Case statement
}# End of switch $Elem
}
# Declare the function that runs each time
# character data is encountered.
function characterHandler($parser, $line) {
global $data;
global $cdata;
global $name;
# Place lines into an array because elements
# can contain more than one line of data.
$cdata[] = $line;
}# End of function CharacterHandler
?>
Does this work? See it in action.
|
|
Get The FREE Software:
Company Summary:
- Incorporated in 1995
- Consulting Services to Fortune 500 companies
- Developing software since 1980
- Developer Member of ASP
|