Skip to main content

Run SQL statements on XML documents

Project description

AskXML

Run SQL statements on XML documents

<xml>
    <fruit color='green'>tasty kiwi</fruit>
    <fruit color='dark green'>old kiwi</fruit>
    <fruit color='green'>apple</fruit>
</xml>
>>> 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()

BUT WHY?

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.

Installation

A pip package is on it's way.

Usage

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 = [
    Table('fruit',
        Column('age', Integer()),
        Index('age'))
]
with AskXML('file.xml', table_definitions=tables) as conn:
    c = conn.cursor()
    c.execute("UPDATE fruit SET age = 5 WHERE _text = 'tasty kiwi'")
    c.close()

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:

<xml>
    <someParent name='Jerry'>
        <someChild name='Morty' />
        <someChild name='Summer' />
    </someParent>
</xml>
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 parent.name = 'Jerry'
    """)
    for row in results.fetchall():
        print(row)
    c.close()

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)")

License

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.

Source Distribution

askxml-1.0.0.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

askxml-1.0.0-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

Details for the file askxml-1.0.0.tar.gz.

File metadata

  • Download URL: askxml-1.0.0.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for askxml-1.0.0.tar.gz
Algorithm Hash digest
SHA256 5ab887bffaaa6af45fd35d459093a8cb492db256f82b290a855d3a7aec1f7856
MD5 0b71fb85f0e12360f804887164a46308
BLAKE2b-256 0476ce523e19e4b11a364c5dcc6f7e3da5cdc42b27485be7482bb3dfd6d4708d

See more details on using hashes here.

File details

Details for the file askxml-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for askxml-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9908b6b35baba2c9a1ae028091c94bc109587b7e84b448e86047bc5e2ddd2f93
MD5 f8304205cbfb1a3bbd4bbf2ea9cef8a9
BLAKE2b-256 5c7e3abbd91cf720ec33cefce91a57064b7044fec50218ffcec3f5de0f115697

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page