Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

uxml

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

uxml

uRPC is oversimplistic stream XML parser

  • 0.3.0
  • PyPI
  • Socket score

Maintainers
1

μ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.

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.

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

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc