#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2002
    Posts
    1
    Rep Power
    0

    Unhappy Bloody Stupid Question #1


    Sorry for this, but we all have to start somewhere...

    Using PHP and MySQL, how do I get data from a XML document into the database? I know by using DOM, but does that mean that the DOM parser will write all the data to the database?!? Or does it store it in variables?!?

    Help guys! I'm feeling pretty damn stupid here, but need a bunch of stupid questions answered.

    Best

    Stiaan
  2. #2
  3. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Oct 2001
    Location
    New Zealand
    Posts
    1,774
    Rep Power
    24
    Read the XML file into arrays then do sql statements. There may be another way (probably) but that way serves me best. Something like:

    PHP Code:
        $file "newsXML.xml";
        
    $newsitem = array();
        
    $id ""$text "";

        function 
    startElement($parser$name$attribs) {
            global 
    $id;
            if (
    $name == 'NEWSITEM') {
                
    $id $attribs['ID'];
            }        
        }

        function 
    endElement($parser$name) {
            global 
    $id;
            global 
    $text;
            global 
    $newsitem;
            if (
    $name == 'HEADLINE') {
                
    $newsitem[$id][0] = $text;
                
    $text "";
            }
            if (
    $name == 'NEWSTEXT') {
                
    $newsitem[$id][1] = $text;
                
    $text "";
            }

        }
        function 
    characterData($parser$data) {
            global 
    $text;
            
    $text .= $data;
        }


        
    $xml_parser xml_parser_create();
        
    xml_set_element_handler($xml_parser"startElement""endElement");
        
    xml_set_character_data_handler($xml_parser"characterData");

        
    $fp fopen($file"r");

        while (
    $data fread($fp4096)) {
                
    xml_parse($xml_parser$datafeof($fp));
          }
        
    xml_parser_free($xml_parser); 
    That would give you an array of:
    headline : $newsitem[id][0]
    text of news item : $newsitem[id][1]

    Then do a foreach()

    to put it into a database.

IMN logo majestic logo threadwatch logo seochat tools logo