Softpedia
 


LINUX CATEGORIES:



GLOBAL PAGES >>
NEWS ARCHIVE >>
SOFTPEDIA REVIEWS >>
MEET THE EDITORS >>
WEEK'S BEST
  • Linux Kernel 3.9.6 / 3....
  • Linux Kernel 3.0.82 LTS...
  • KDE Software Compilatio...
  • PulseAudio 4.0
  • Wireshark 1.10.0
  • NetworkManager 0.9.8.2
  • LibreOffice 3.6.6 / 4.0...
  • SystemRescueCd 3.7.0
  • Linux Kernel 3.10 RC6
  • Ubuntu Tweak 0.8.5
  • Home > Linux > Programming > Libraries

    Elementflow 0.4.2

    Download button

    No screenshots available
    Downloads: 335  View global page NEW!  Tell us about an update
    User Rating:
    Rated by:
    NOT RATED
    0 user(s)
    Developer:

    License / Price:

    Last Updated:

    Category:
    Ivan Sagalaev | More programs
    BSD License / FREE
    November 30th, 2010, 10:20 GMT
    ROOT / Programming / Libraries

     Read user reviews (0)  Refer to a friend  Subscribe

    Elementflow description

    Streaming XML generator for Python

    Elementflow is a Python library for generating XML as a stream without first building a tree in memory.

    Some existing XML producing libraries (like ElementTree, lxml) build a whole XML tree in memory and then serialize it. It might be inefficient for moderately large XML payloads (think of a content-oriented Web service producing lots of XML data output). Python's built-in xml.sax.saxutils.XMLGenerator is very low-level and requires closing elements by hand.

    Also, most XML libraries, to be honest, suck when dealing with namespaces.

    Usage:

    Basic XML generation:

    import elementflow
    file = open('text.xml', 'w') # can be any object with .write() method

    with elementflow.xml(file, u'root') as xml:
     xml.element(u'item', attrs={u'key': u'value'}, text=u'text')
     with xml.container(u'container', attrs={u'key': u'value'}):
     xml.text(u'text')
     xml.element(u'subelement', text=u'subelement text')


    Using with is required to properly close container elements. The library expects unicode strings on input and produces utf-8 encoded output (you may omit those "u"s for purely ASCII strings if you want to, Python will convert the, automatically).

    XML with namespaces:

    with elementflow.xml(file, 'root', namespaces={'': 'urn:n', 'n1': 'urn:n1'}) as xml:
     xml.element('item')
     with xml.container('container', namespaces={'n2': 'urn:n2'):
     xml.element('n1:subelement')
     xml.element('n2:subelement')

    Elements with namespaces are defined using prefixes. You can define namespaces at the root level and for any container. The library will check for namespace prefixes that wasn't defined beforehand and will raise ValueError in that case.

    Pretty-printing is also supported:

    with elementflow.xml(file, 'root', indent=True):
     # ...


    In some cases it's more convenient to have such XML producer as a Python iterator. This is easily done by wrapping XML generation code into a generator function:

    def g():
     xml = elementflow.xml(elementflow.Queue(), 'root')
     with xml:
     for item in collection:
     xml.element(...)
     yield xml.file.pop()
     yield xml.file.pop()


    elementflow.Queue() is a temporary buffer that accepts data from an XML generator and is cleared upon calling .pop() on it. You also might want to yield data from the iterator only when this buffer reaches a certain size:

    if len(xml.file) > BUFSIZE:
     yield xml.file.pop()



    Product's homepage

    Requirements:

    · Python

      


    TAGS:

    XML generator | streaming XML | Python library | streaming | XML | generator

    Go to top

    WindowsGamesDriversMacLinuxScriptsMobileHandheldNews

    SUBMIT PROGRAM   |   ADVERTISE   |   GET HELP   |   SEND US FEEDBACK   |   RSS FEEDS   |   UPDATE YOUR SOFTWARE   |   ROMANIAN FORUM