uRPC is oversimplistic stream XML parser
Project description
# μXML - xml stream parsing in less than 100 lines of code
μXML is oversimlified way to parse large (or slow loading) XML lines with easy way for tag matching and callbacking. No dependencies (besides python3.4+) and only one class uxml.Parser inside the library.
`python import uxml p = uxml.Parser(open('test.rss')) p.find('/rss/channel/item', lambda e: print(e)) p.start() `
First meaningful line is parser’s object creation. The only parameter is file (or any other io.TextIOWrapper compatible) object. .find method add new tag matcher for parser. First argument is a pointer to tag which we want to find inside xml stream. I use nanoxpath notation (i.e. you should show full path to this tags or use // as a starting symbol). Second argument of find is a callback which will be invoked if given tag is found and the only argument for this callback is a internal structure of tags and attributes inside this tag. Attributes can be separated from children tags by name, it’s always starts with ‘@’ symbol. If tag is just one internal text - it will be returned as result. If there’s more than one children with same name, it will be returned as list of tags. If tag have both internal text and attributes, first will be added to attributes as ‘#text’.
You can use unlimited number of find’s. Parsing will start with start method. Example above will print every item inside given rss file. Let me show you a little bit more complicated stuff.
`python res = [] p = uxml.Parser('test.rss') p.find('//link', lambda x: res.append(x)).find('//description', lambda x: print(x)).start() `
First off all you can notice what find and start calls are chainable. Trailing / will convert to any number of previous tags, so this time we will got list of links at res variable. Also we just pass ‘test.rss’ filename to Parser constructor. It’s not only open this file but also trying to guess XML encoding (very poorly but anyway).
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
File details
Details for the file uxml-0.3.0.tar.gz
.
File metadata
- Download URL: uxml-0.3.0.tar.gz
- Upload date:
- Size: 3.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 76e7d755e03b74b127b3a460c6e28dea96b61caa3fff3e879d302e784923ceb7 |
|
MD5 | b9cecd8e9a82848b7c575c90680aee77 |
|
BLAKE2b-256 | 9354c9fdb746cd474f46b61b0f0ba6aad8fa906fed10243e22c67b6228e79e89 |