繁体   English   中英

Python lxml objectify - 如何使用破折号创建元素

[英]Python lxml objectify - How to create elements with a dash

我需要使用lxml的objectify模块来创建一些在其中包含破折号的xml元素。 例如:

<program-id>$Id: myFile.py 3519 2012-07-17 13:37:20Z $</program-id>
<formatter-txt>basic format</formatter-txt>

我在网上找不到任何有关如何执行此操作的参考资料,当我尝试在Python中执行此操作时,这是一个语法错误。 任何帮助,将不胜感激。

使用这里的文档因为我从未使用过客观化:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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