Import XML into SQL database

12,127

Solution 1

You can use the getiterator() function to iterate over the XML file without parsing the whole thing at once. You can do this with ElementTree, which is included in the standard library, or with lxml.

for record in root.getiterator('record'):
    add_element_to_database(record) # Depends on your database interface.
                                    # I recommend SQLAlchemy.

Solution 2

I've done this several times with Python, but never with such a big XML file. ElementTree is an excellent XML library for Python that would be of assistance. If it was possible, I would divide the XML up into smaller files to make it easier to load into memory and parse.

Share:
12,127
Jacob Lyles
Author by

Jacob Lyles

Updated on June 24, 2022

Comments

  • Jacob Lyles
    Jacob Lyles less than a minute

    I'm working with a 20 gig XML file that I would like to import into a SQL database (preferably MySQL, since that is what I am familiar with). This seems like it would be a common task, but after Googling around a bit I haven't been able to figure out how to do it. What is the best way to do this?

    I know this ability is built into MySQL 6.0, but that is not an option right now because it is an alpha development release.

    Also, if I have to do any scripting I would prefer to use Python because that's what I am most familiar with.

    Thanks.