Skip to content

Instantly share code, notes, and snippets.

@diminoten
Created January 5, 2015 18:33
Show Gist options
  • Save diminoten/bf4af61398689464f2d3 to your computer and use it in GitHub Desktop.
Save diminoten/bf4af61398689464f2d3 to your computer and use it in GitHub Desktop.
import lxml.builder as builder
from lxml import etree
foo = builder.E.foo
bar = builder.E.bar
bing = builder.E.bing
foo_xml = foo(
bar(
bing("Some nested thing."),
more="nested things",
even="more nested things"
),
attribute_one="animal",
attribute_two="plant",
xmlns="https://url.to.github.project"
)
print etree.tostring(foo_xml, pretty_print=True)
@diminoten
Copy link
Author

Comes out as:

<foo attribute_one="animal" xmlns="https://url.to.github.project" attribute_two="plant">
  <bar even="more nested things" more="nested things">
    <bing>Some nested thing.</bing>
  </bar>
</foo>

@diminoten
Copy link
Author

The problem might arise if I needed to set one of my element tag names to a keyword, e.g.:

set = builder.E.set

set_xml = set(
    attribute="foo"
)

But I think I can just do this:

different_set = builder.E.set

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment