Parsing Do-Not-Call-Registry XML files . . . anyone?

Discussion in 'PHP' started by WebGyver, Apr 15, 2008.

  1. #1
    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):

     
    WebGyver, Apr 15, 2008 IP
  2. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #2
    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
     
    jayshah, Apr 15, 2008 IP
  3. LittleJonSupportSite

    LittleJonSupportSite Peon

    Messages:
    386
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #3
    
    
    <?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.
     
    LittleJonSupportSite, Apr 15, 2008 IP
  4. WebGyver

    WebGyver Guest

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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!
     
    WebGyver, Apr 15, 2008 IP