xml文件示例:
<header>
<name>name</name>
<items>
<item>
<title>title</title>
<add>add</add>
</item>
<item>
<title>title</title>
<add>add</add>
</item>
</items>
</header>
我想将信息解析为按每个标题和子组项分组的组:
xml解析:
name
----title
----add
----title
----add
next header
name
----tile
----add
----etc
----etc
如果有人可以发布一个例子,最好使用elem tree iterparse 它是一个大型的xml文件...
我的例子不起作用是......
import xml.etree.cElementTree as etree
infile = open("c:/1.xml", 'r')
context = etree.iterparse(infile, events=("start", "end"))
for event, element in context:
if event == "end":
if element.tag == "header":
print element.findtext('name')
elif element.tag == "item":
print element.findtext('title')
print element.findtext('add')
答案 0 :(得分:3)
所以,很好,简单,你提供的infile:
import xml.etree.cElementTree as etree
for event, element in etree.iterparse("C:/1.xml"):
if element.tag == "name":
print element.text
elif element.tag in ["title", "add"]:
print "---" + element.text
这给出了输出:
name
----title
----add
----title
----add
我想如果你想在标题之间插入间隔符,你只需要:
if element.tag == "header":
print "\n"