Has anyone had any experience with the processing of XML (or flat-text) files of area codes and phone numbers from the National Do Not Call Registry? I'm having a heck of a time, trying to get the contents of an external XML file into a MySQL database. No, I do not want to insert the entire 25 MB file into a single column -- rather, I would like to insert the values of <ac> and <ph> nodes into the corresponding columns in the database table. The trouble is, I have to do this once a month, so I'd much rather figure out how to make it work in PHP than do this manually. Any ideas, links or suggestions? Here's what the XML file looks like (in a condensed example, of course): <list type='full' level='state' val='UT'> <ac val='435'> <ph val='2000071' /> <ph val='2000072' /> <ph val='2000074' /> <ph val='9949900' /> <ph val='9949999' /> </ac> <ac val='801'> <ph val='2010000' /> <ph val='2010014' /> <ph val='9998900' /> <ph val='9999999' /> </ac> </list> Code (markup):
If you have PHP5, you should consider looking up SimpleXML (first), then DOM (Document Object Model). It should help you quite a bit here. Jay
<?php $file = "your file.xml"; function contents($parser, $data){ echo $data; } function startTag($parser, $data){ echo "<b>"; } function endTag($parser, $data){ echo "</b><br />"; } $xml_parser = xml_parser_create(); xml_set_element_handler($xml_parser, "startTag", "endTag"); xml_set_character_data_handler($xml_parser, "contents"); $fp = fopen($file, "r"); $data = fread($fp, 80000); if(!(xml_parse($xml_parser, $data, feof($fp)))){ die("Error on line " . xml_get_current_line_number($xml_parser)); } xml_parser_free($xml_parser); fclose($fp); ?> PHP: Instead of outputting <b>blah</b><br> You could simply make that sql[] The for each sql as $x do your insert into the DB.
Yo, Little Jon, that's awesome! Thank you very much. I'll give this a try and do as you suggested. Thanks for giving me a headstart! Much appreciated!