- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 Django 中,
addQuickElement(name,content,attr)
生成这样的XML
<name attr="attr">content</name>
虽然我想生成
<name attr="attr" />
最佳答案
只是不要指定 contents
参数。
作为引用,这是django/utils/xmlutils.py
:
"""
Utilities for XML generation/parsing.
"""
from xml.sax.saxutils import XMLGenerator
class SimplerXMLGenerator(XMLGenerator):
def addQuickElement(self, name, contents=None, attrs=None):
"Convenience method for adding an element with no children"
if attrs is None: attrs = {}
self.startElement(name, attrs)
if contents is not None:
self.characters(contents)
self.endElement(name)
您可以在这里看到,您只需要不指定contents
,因此您可以执行x.addQuickElement(name, attrs=attrs)
。
(快速查看 XMLGenerator 表明这仍会生成结束标记,而不是自结束标记。在 Python 3.2 中,参数 short_empty_elements
已添加到 XMLGenerator.__init__
,但 Django 仍然不仅仅与 Python 2.x 兼容。如果您关心获得短标记,请查看 xml.sax.saxutils.XMLGenerator.startElement
实现。)
关于python - 如何使用 XMLGenerator 生成空的封闭元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8455704/
在 Django 中, addQuickElement(name,content,attr) 生成这样的XML content 虽然我想生成 最佳答案 只是不要指定 contents 参数。 作为引
这是 Java 问题 How to output a CDATA section from a Sax XmlHandler 的 Python 等价物 都不是xml.sax.saxutils.XMLG
我使用 XMLGenerator 来编写内容,但似乎不知道如何使用命名空间。我不断收到 KeyErrors。 作为一个简单的例子,我想写 我应该如何调用startElementNS? xml
我使用 xml.sax.saxutils 中的 python XMLGenerator 类写出 xml . xmlwriter = XMLGenerator(output, encoding)
我是一名优秀的程序员,十分优秀!