Reputation: 33071
I have a need to use lxml's objectify module to create some xml elements that have a dash in them. For example:
<program-id>$Id: myFile.py 3519 2012-07-17 13:37:20Z $</program-id>
<formatter-txt>basic format</formatter-txt>
I can't find any references online about how to do this and when I try to just do it in Python, it's a syntax error. Any help would be appreciated.
Upvotes: 0
Views: 1438
Reputation: 38247
Using the documentation here as I've never used objectify:
>>> from lxml import objectify
>>> doc = objectify.E.xml()
>>> doc.append(getattr(objectify.E,'program-id')("$Id: myFile.py 3519 2012-07-17 13:37:20Z $"))
>>> doc.append(getattr(objectify.E,'formatter-text')("basic format"))
>>> from lxml import etree
>>> print etree.tostring(doc,pretty_print=True)
<xml xmlns:py="http://codespeak.net/lxml/objectify/pytype" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<program-id py:pytype="str">$Id: myFile.py 3519 2012-07-17 13:37:20Z $</program-id>
<formatter-text py:pytype="str">basic format</formatter-text>
</xml>
Upvotes: 2