Giter Site home page Giter Site logo

askxml's Introduction

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

AskXML requires Python 3.5+. Best way to install is to get it with pip:

pip install askxml

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

Contributing

Any contributions are welcome.

License

AskXML is licensed under MIT license

askxml's People

Contributors

kamac avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.