简体   繁体   中英

Python lxml objectify - How to create elements with a dash

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM