Skip to main content

Run SQL statements on XML documents

Project description


Run SQL statements on XML documents

    <fruit color='green'>tasty kiwi</fruit>
    <fruit color='dark green'>old kiwi</fruit>
    <fruit color='green'>apple</fruit>
>>> from askxml import AskXML

>>> conn = AskXML('file.xml')
# get an SQL cursor object
>>> c = conn.cursor()
>>> results = c.execute("SELECT color FROM fruit WHERE _text LIKE '% kiwi'")
>>> for row in results.fetchall():
>>>    print(row)
[('green'), ('dark green')]

# cleanup
>>> c.close()
>>> conn.close()


There are data dumps like stack exchange's, stored in XML. They're big, so fitting them whole into memory is not desired. With AskXML you can query things fast, and rather comfortably (provided you know SQL).

Before you go any further though, it's very possible your task can be achieved with XPATH and ElementTree XML API, so give that a look if you haven't heard of it.


A pip package is on it's way.


Adding indexes and defining columns

If you want to add indexes, columns or set attribute types, you can pass a list of table definitions:

from askxml import *
tables = [
        Column('age', Integer()),
with AskXML('file.xml', table_definitions=tables) as conn:
    c = conn.cursor()
    c.execute("UPDATE fruit SET age = 5 WHERE _text = 'tasty kiwi'")

You don't need to define all existing columns or tables. If a definition was not found, it's created with all column types being Text by default.

Node hierarchy

If you want to find nodes that are children of another node by attribute:

    <someParent name='Jerry'>
        <someChild name='Morty' />
        <someChild name='Summer' />
from askxml import *
with AskXML('file.xml') as conn:
    c = conn.cursor()
    results = c.execute("""
        SELECT name FROM someParent_someChild as child
        INNER JOIN someParent as parent ON parent._id = child._parentId
        WHERE = 'Jerry'
    for row in results.fetchall():

This will print [('Morty'), ('Summer')].

Inserting new data

If you want to add a new tag:

cursor.execute("INSERT INTO fruit (color, _text) VALUES ('red', 'strawberry')")

Or if your nodes have a hierarchy:

cursor.execute("INSERT INTO someParent_someChild (name, _parentId) VALUES ('a baby', 1)")


AskXML is licensed under MIT license

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for askxml, version 1.0.0
Filename, size File type Python version Upload date Hashes
Filename, size askxml-1.0.0-py3-none-any.whl (10.5 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size askxml-1.0.0.tar.gz (8.3 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page