Code Monkey home page Code Monkey logo

txml's Introduction

txml

txml is a module to parse XML files to a json-like object

Usage

Reading and searching throug an .xml file or string Using 'sample.xml'

>>> from txml import XmlParser
>>> source = 'sample.xml'
>>> parser = XmlParser(source=source)
>>> products = parser.search_nodes(tag='controller')

The products object is a generator of all matched instances of the tag controller. The results can only be accessed once. If the results require multiple accesses then the generator should be converted to a list. If no parameters are passed to the method, the entire xml-source will be returned in the

>>> product_list = list(product)
>>> len(product_list)
3

Let's look at the first entry. A dictionary of the element is returned containing the elements attributes in a nested dictionary elem and a dictionary containing the a list of all of the elements child nodes in children. Even sub nodes(grandchildren) are returned.

>>> product_list[0]
{'element': {'attr': {
                      'type': 'usb',
                      'index': '0'},
             'text': '\n            ',
             'tag': 'controller'}
 'children': [{'children': [],
               'element': {
                            'attr': {'name': 'usb0'},
                            'text': None,
                            'tag': 'alias'}},
              {'children': [],
               'element': {
                        'attr': {'type': 'pci', 'domain': '0x0000',
                                 'bus': '0x00', 'slot': '0x01',
                                 'function': '0x2'},
                        'text': None,
                        'tag': 'address'}}] }

The txml module can also search for node which match a set of attributes. Any number of attributes can be passed and the function will return any node containing the matching attributes.

>>> product = parser.search_node_attr(tag='controller', type='usb')
>>> len(list(product))
1
>>> product
{'elem': {'type': 'usb', 'index': '0',
          'text': '\n            ',
          'tag': 'controller'}
 'children': [{'children': [],
               'elem': {
                        'attr' : {'name': 'usb0'},
                        'text': None, 'tag': 'alias'}},
              {'children': [],
               'elem': {'attr' {
                                'type': 'pci', 'domain': '0x0000',
                                'bus': '0x00', 'slot': '0x01',
                                'function': '0x2'},
                        'text': None,
                        'tag': 'address'}}] }

Installation

Clone this repo, and enjoy.

License

'txml' is released under the terms of the MIT license

Links

To Do

  • Performance tests
  • Support for namespaces

txml's People

Contributors

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