Git Product home page Git Product logo

xml_from_seq's Introduction

xml_from_seq

Generate XML from a Python data structure.

Examples

The XML() function renders a Python list or tuple to an XML element. The first item is the element name, the second item – if it's a dict – is the attributes, and the rest of the items are either text or nested elements.

from xml_from_seq import XML

item = ['item', {'attr1': 123, 'attr2': 'other value'}, 'This is the content of the item.']
assert XML(item) == '<item attr1="123" attr2="other value">This is the content of the item.</item>'

item = [
    'item',
    'This is some content of the item.'
    ['sub', 'This is the content of a subelement.']
]
print(XML(item))
 <item>
    This is some content of the item.
    <sub>
        This is the content of a subelement.
    </sub>
</item>

If an element is False or None it will be omitted. If a element's name is None its contents will appear in its place. If an attribute's value is None it will be omitted.

If an element's name is a list or tuple, it will be inserted into the XML as-is – so you can include already-rendered XML by double-bracketing it:

print(XML([['<foo>123</foo>']]))
<foo>123</foo>

If an element's tag name is xml_from_seq.CDATA, that element's content will be rendered unescaped in a <![CDATA[...]]> section.

Indentation and line breaks

If the first item in an element (not counting an attribute dict) is xml_from_seq.INLINE, that element's contents won't be indented on separate lines from the element's start and end tags.

from xml_from_seq import INLINE, XML

item = [
    'item',
    'This is some content of the item.'
    ['sub', INLINE, 'This is the content of a subelement.']
]
print(XML(item))
 <item>
    This is some content of the item.
    <sub>This is the content of a subelement.</sub>
</item>

You can pass an integer indent parameter to the XML() function to indent the output XML by that many tabs.

xml_from_seq's People

Contributors

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