Jump to content

Upload xml files in a folder to Mysql using Php using glob and array


BruceFarmerson

Recommended Posts

I have several xml files in a folder on my webserver. I want to

1. look in the folder
2. get xml file names
3. put file names into array
4. loop those names into a mysql query
5. have contents of all xml files inserted into db

 

     $files = glob('*.xml');
    
    foreach($files as $file){
        mysql_query("LOAD DATA INFILE '".$file."' INTO TABLE tablename ROWS IDENTIFIED BY ''");
    
    }

 




I've spent a couple of days looking for an example or tutorial but can't find anything online.

Above is the best I can come up with.

Any help appreciated :)

The only time I've ever used this is to load a CSV file into MySQL. I've checked the documentation and I can't see that XML is supported using this function. It looks relatively simple looking for a field delim and new lines. New lines identifying the end of the row. It's worth pointing out that XML does not need to follow this convention to be valid:

 

<parent><child>Im a babeh!</child></parent><parent><child>Im a babeh!</child></parent>

 

The above is valid XML but two records are on the same line. So LOAD DATA INFILE function wouldn't know where one row stops and the other begins.

 

You might be able to fudge around it with adding custom delims but you may encounter problems. The safest solution would be to create a CSV using PHP, you'll have more control over handeling formatting issues and it'd only take a few lines using SimpleXMLIterator and SPLFileObject.

 

If you need any more help then give me a shout

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.