使用ElementTree创建Xml
我是python的新手。我想创建一个父树,几个子树和几个子树的xml树。我存储的子标签在'TAG'列表中,Subchild标签在'SUB'列表中 我想出了以下代码,但是我无法达到预期效果!使用ElementTree创建Xml
def make_xml(tag,sub):
'''
Takes in two lists and Returns a XML object.
The first list has to contain all the tag objects
The Second list has to contain child data's
'''
from xml.etree.ElementTree import Element, SubElement, Comment, tostring
top = Element("Grand Parent")
comment = Comment('This is the ccode parse tree')
top.append(comment)
i=0
try:
for ee in tag:
child = SubElement(top, 'Tag'+str(i))
child.text = str(tag[i]).encode('utf-8',errors = 'ignore')
subchild = SubElement(child, 'Content'+str(i))
subchild.text = str(sub[i]).encode('utf-8',errors = 'ignore')
i = i+1;
except UnicodeDecodeError:
print 'oops'
return top
编辑: 我有两个列表喜欢这些: TAG = [ '高兴', 'GO', 'LUCKY'] SUB = [ 'ED', 'EDD', '涡']
我要的是:
<G_parent>
<parent1>
HAPPY
<child1>
ED
<\child1>
<\parent1>
<parent2>
GO
<child2>
EDD
<\child2>
<\parent2>
<parent3>
LUCKY
<child3>
EDDY
<\child3
<\parent3>
<\G_parent>
实际列表有很多比这更多的内容。我想实现使用for循环左右。
EDIT:
OOP's。我的错 ! 代码正常工作,当我通过示例列表。但在我的真实应用中,列表很长。该列表包含从pdf文件中提取的文本片段。在那个文本的某处,我得到了UnicodeDecodeError(原因:pdf提取的文本杂乱。证明:'oops'打印一次)并且返回的xml对象不完整。 所以我需要找出即使在UnicodeDecodeErrors我的完整列表解析。那可能吗 !我正在使用.decode('utf-8',errors ='ignore'),即使解析没有完成!
请参阅this article,尤其是构建XML文档部分。
+1,因为我希望鼓励你关注etree的替代方案,并进行后续工作;-) – 2012-07-05 14:59:00
@DonQuestion:谢谢,但你不可能很快看到我这样做,因为我是维护者etd在stdlib :) – 2012-07-05 17:29:18
预期结果是什么?正如我所见,这返回元素和子元素的XML。 – Tisho 2012-07-05 14:22:59
请编辑您的原始文章,以显示少量您想要编码的内容以及它的外观。 – octopusgrabbus 2012-07-05 14:28:28