用python迭代编写XML节点 [英] Iteratively write XML nodes in python

查看:152
本文介绍了用python迭代编写XML节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有多种读取XML的方法,既可以一次(DOM),也可以一次一位(SAX).我已经使用SAX或lxml来迭代读取大型XML文件(例如Wikipedia转储,压缩后为6.5GB).

There are many ways to read XML, both all-at-once (DOM) and one-bit-at-a-time (SAX). I have used SAX or lxml to iteratively read large XML files (e.g. wikipedia dump which is 6.5GB compressed).

但是,在对该XML文件进行了一些迭代处理(在python中使用ElementTree)之后,我想将(新)XML数据写到另一个文件中.

However after doing some iterative processing (in python using ElementTree) of that XML file, I want to write out the (new) XML data to another file.

是否有用于迭代写出XML数据的库?我可以创建XML树,然后将其写出来,但是如果没有ram,那是不可能的.是否有将XML树迭代地写入文件的方法?一次一次?

Are there any libraries to iteratively write out XML data? I could create the XML tree and then write it out, but that is not possible without oodles of ram. Is there anyway to write the XML tree to a file iteratively? One bit at a time?

我知道我可以自己用print "<%s>" % tag_name等生成XML,但这似乎有点... hacky .

I know I could generate the XML myself with print "<%s>" % tag_name, etc., but that seems a bit... hacky.

推荐答案

Fredrik Lundh的 elementtree .SimpleXMLWriter 可让您逐步写出XML.这是模块中嵌入的演示代码:

Fredrik Lundh's elementtree.SimpleXMLWriter will let you write out XML incrementally. Here's the demo code embedded in the module:

from elementtree.SimpleXMLWriter import XMLWriter
import sys

w = XMLWriter(sys.stdout)

html = w.start("html")

w.start("head")
w.element("title", "my document")
w.element("meta", name="generator", value="my application 1.0")
w.end()

w.start("body")
w.element("h1", "this is a heading")
w.element("p", "this is a paragraph")

w.start("p")
w.data("this is ")
w.element("b", "bold")
w.data(" and ")
w.element("i", "italic")
w.data(".")
w.end("p")

w.close(html)

这篇关于用python迭代编写XML节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆