Skip to content

am230/py2xml

Repository files navigation

Writing XML with Pythonic code

Installation

$ pip install py2xml

Usage

import py2xml

# typing attributes
@py2xml.to_element
def note(id: int): ...


messages = py2xml.Element("messages")
to = py2xml.Element("to")
from0 = py2xml.Element("from")
heading = py2xml.Element("heading")
body = py2xml.Element("body")
print(
    messages[
        note(id=501)[
            to[
                "Tove"
            ],
            from0[
                "Jani"
            ],
            heading[
                "Reminder"
            ],
            body[
                "Don't forget me this weekend!"
            ]
        ],
        note(id=502)[
            to[
                "Jani"
            ],
            from0[
                "Tove"
            ],
            heading[
                "Re: Reminder"
            ],
            body[
                "I will not"
            ]
        ]
    ]
)

<messages><note id="501"><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note><note id="502"><to>Jani</to><from>Tove</from><heading>Re: Reminder</heading><body>I will not</body></note></messages>

to xml ElementTree

Element.to_xml()

generate code

import py2xml

print(py2xml.util.generate_code('''<messages>
<note id="501">
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don't forget me this weekend!</body>
</note>
<note id="502">
    <to>Jani</to>
    <from>Tove</from>
    <heading>Re: Reminder</heading>
    <body>I will not</body>
</note>
</messages>'''))
messages = py2xml.Element("messages")
note = py2xml.Element("note")
to = py2xml.Element("to")
from0 = py2xml.Element("from")
heading = py2xml.Element("heading")
body = py2xml.Element("body")
messages[
note(id="501")[
to[
"Tove"
],
from0[
"Jani"
],
heading[
"Reminder"
],
body[
"Don't forget me this weekend!"
]
],
note(id="502")[
to[
"Jani"
],
from0[
"Tove"
],
heading[
"Re: Reminder"
],
body[
"I will not"
]
]
]

About

Write XML with Pythonic Code

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages