import a structured xml file into Mysql database

Discussion in 'MySQL' started by Vonzep, Apr 9, 2008.

  1. #1
    Hi everyone, i hope i can find the solution for my problem here.
    I have to import a structured xml file to mysql database. the xml file contains economic information of the company (sells/transactions, etc etc) and in the php site diferent kinds of information are viewed for diferent types of users...
    I hope i've made the question clear and pardon my english :D
     
    Vonzep, Apr 9, 2008 IP
  2. Dade72

    Dade72 Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    This question should actually have been asked in the PHP section, since I don't think it would be smart to try to accomplice this with javascript, but here is a php code that might help:

    
    function insertIntoMySQL($filename, $tablename) {
          $xml_file = fopen($filename, "r"); 
          $this->xml->read_file_handle($xml_file);
              
          $numRows = $this->xml->roottag->num_subtags();
    	  
          for ($i = 0; $i < $numRows; $i++) {
               $arrFields = null;
    		   $arrValues = null; 
    
    		   $row = $this->xml->roottag->tags[$i];
               $numFields = $row->num_subtags();
    
               for ($ii = 0; $ii < $numFields; $ii++) {
     	          $field = $row->tags[$ii];
                  $arrFields[] = $field->name;
                  $arrValues[] = "\"".$field->cdata."\"";
               }
    
               $fields = join($arrFields, ", ");
               $values = join($arrValues, ", ");
    
               $this->recordSet->exec("Insert Into $tablename ($fields) Values ($values)");
          }
       }
    
    PHP:
     
    Dade72, Apr 10, 2008 IP