从 Python 编写 XML:Python 相当于 .NET XmlTextWriter?

发布于 2024-07-25 06:18:32 字数 436 浏览 4 评论 0 原文

我有一些 IronPython 代码,它使用 XmlTextWriter,它允许我编写代码,就像

self.writer = System.Xml.XmlTextWriter(filename, None)
self.writer.Formatting = Formatting.Indented
self.writer.WriteStartElement(name)
self.writer.WriteString(str(text))
self.writer.WriteEndElement()

...

self.writer.Close()

我想让我的代码在 Python 实现(CPython、IronPython 和 Jython)之间移植一样。 是否有一个流式 Python XML 编写器可以用于此目的,而无需使用打印语句,或者在将其写入文件之前构建整个 DOM 树?

I have some IronPython code which makes use of XmlTextWriter which allows me to write code like

self.writer = System.Xml.XmlTextWriter(filename, None)
self.writer.Formatting = Formatting.Indented
self.writer.WriteStartElement(name)
self.writer.WriteString(str(text))
self.writer.WriteEndElement()

...

self.writer.Close()

I would like to make my code portable across Python implementations (CPython, IronPython and Jython). Is there a streaming Python XML writer I can use for this without needing to use either print statements, or to construct a whole DOM tree before writing it out to file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寂寞清仓 2024-08-01 06:18:32

我编写了一个名为 loxun 的模块来做到这一点: http://pypi.python.org/pypi/乐讯/。 它可以与 CPython 2.5 和 Jython 2.5 一起运行,但我从未尝试过使用 IronPython。

用法示例:

with open("...", "wb") as out:
  xml = XmlWriter(out)
  xml.addNamespace("xhtml", "http://www.w3.org/1999/xhtml")
  xml.startTag("xhtml:html")
  xml.startTag("xhtml:body")
  xml.text("Hello world!")
  xml.tag("xhtml:img", {"src": "smile.png", "alt": ":-)"})
  xml.endTag()
  xml.endTag()
  xml.close()

结果:

<?xml version="1.0" encoding="utf-8"?>
<xhtml:html xlmns:xhtml="http://www.w3.org/1999/xhtml">
  <xhtml:body>
    Hello world!
    <xhtml:img alt=":-)" src="smile.png" />
  </xhtml:body>
</xhtml:html>

除其他功能外,它还可以在您编写时检测未对齐的标签,使用内存占用较小的流 API,支持 Unicode 并允许禁用漂亮打印。

I wrote a module named loxun to do just that: http://pypi.python.org/pypi/loxun/. It runs with CPython 2.5 and Jython 2.5, but I never tried it with IronPython.

Example usage:

with open("...", "wb") as out:
  xml = XmlWriter(out)
  xml.addNamespace("xhtml", "http://www.w3.org/1999/xhtml")
  xml.startTag("xhtml:html")
  xml.startTag("xhtml:body")
  xml.text("Hello world!")
  xml.tag("xhtml:img", {"src": "smile.png", "alt": ":-)"})
  xml.endTag()
  xml.endTag()
  xml.close()

And the result:

<?xml version="1.0" encoding="utf-8"?>
<xhtml:html xlmns:xhtml="http://www.w3.org/1999/xhtml">
  <xhtml:body>
    Hello world!
    <xhtml:img alt=":-)" src="smile.png" />
  </xhtml:body>
</xhtml:html>

Among other features, it detects missalligned tags while you write, uses a streaming API with a small memory footprint, supports Unicode and allows to disable pretty printing.

彻夜缠绵 2024-08-01 06:18:32

我从未使用过您正在谈论的 .NET 实现,但听起来您将获得的最接近的是 Python 的 SAX 解析器(具体来说,XMLGenerator 类 -- 一些示例代码此处)。

I've never used the .NET implementation you're talking about, but it sounds like the closest you're going to get is Python's SAX parser (specifically, the XMLGenerator class -- some sample code here).

断舍离 2024-08-01 06:18:32

我编写了一个工具来促进从 Python 生成 XML(代码 和 < a href="http://www.from.bz/2009/03/28/announcing-xmlegant-for-python/" rel="nofollow noreferrer">教程)

I wrote a tool to facilitate XML generation from Python (code and tutorial)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文