Bulk Upsert XML with php and Postgresql



I have directories with xml files. Each XML file looks similar to this:



<songs>
<song>
<title>Some Title</title>
<artist>Artist</artist>
</song>
<song>
<title>Other Title</title>
<artist>Artist 1</artist>
</song>
<song>
<title>Some Title</title>
<artist>Artist/artist>
</song>


My Postgresql table have three columns (title, artist, date of insert). I'm using RecursiveDirectoryIterator to iterate through all xml files.


QUESTIONS:




  1. What will be the best approach to accomplish bulk Upsert (the upsert will be insert if not exists and update the date of insert if exist. The distinct is artist and title pair)?




  2. Is there any way to pass the XML directly to the Postgresql by getting it with:



    simplexml_load_file


    Or i've to get it as string with:



    file_get_contents


    And then parse it back to XML within the Postgresql.




I've tried Inserting by Rows from the XML but this will be extremely inefficient when having lets say 100XML files with 100 songs each. So I'm trying to wrap my head about the way to Bulk Upsert on File level, but I'm stuck..


ANY help will be highly appreciated...


Tiada ulasan:

Catat Ulasan