使用xml.etree (请使用此模块)
我如何解析:
<?xml version="1.0" encoding="UTF-8"?>
<EntityPath="c:\a.zip" Name="a.zip" >
<WorkfileDescription>something</WorkfileDescription>
<Revision EntityPath="c:\a.zip" Name="1.1" Author="me">
<ChangeDescription>Some comentary</ChangeDescription>
<PGROUP Name="A" />
<PGROUP Name="B" />
<PGROUP Name="C" />
<Label Name="SOFTWARE" />
<Label Name="READY" />
</Revision>
<Revision EntityPath="c:\a.zip" Name="1.0" Author="me">
<ChangeDescription>Some comentary</ChangeDescription>
<PGROUP Name="A" />
<Label Name="GAME" />
<Label Name="READY" />
</Revision>
</VersionedFile>为了得到:
Revision: a.zip
Name: 1.1
Author: me
ChangeDescription: Some comentary
PGROUP: A
PGROUP: B
PGROUP: C
Label: SOFTWARE
Label: READY
Revision: a.zip
Name: 1.0
Author: me
ChangeDescription: Some comentary
PGROUP: A
Label: GAME
Label: READY到目前为止,使用下面的代码,我只能获得修订行,但我很难解析其他子字段:
from xml.etree import ElementTree
try:
tree = ElementTree.parse(self.xml)
root = tree.getroot()
info_list = []
for child in root:
print(child.tag,child.attrib)
except Exception:
raise
finally:
self.xml = None发布于 2014-05-02 15:38:31
查找所有Revision标记,打印来自element.attrib的所有属性,遍历Revision元素以获得子元素和Name属性值:
import xml.etree.ElementTree as etree
data = """<?xml version="1.0" encoding="UTF-8"?>
<VersionedFile EntityPath="c:\\a.zip" Name="VfOMP_CRM.zip">
<WorkfileDescription>something</WorkfileDescription>
<Revision EntityPath="c:\\a.zip" Name="1.1" Author="me">
<ChangeDescription>Some comentary</ChangeDescription>
<PGROUP Name="A" />
<PGROUP Name="B" />
<PGROUP Name="C" />
<Label Name="SOFTWARE" />
<Label Name="READY" />
</Revision>
<Revision EntityPath="c:\\a.zip" Name="1.0" Author="me">
<ChangeDescription>Some comentary</ChangeDescription>
<PGROUP Name="A" />
<Label Name="GAME" />
<Label Name="READY" />
</Revision>
</VersionedFile>
"""
tree = etree.fromstring(data)
for revision in tree.findall('Revision'):
for key, value in revision.attrib.iteritems():
print "%s: %s" % (key, value)
for child in revision:
print "%s: %s" % (child.tag, child.attrib.get('Name', ''))
print指纹:
Name: 1.1
EntityPath: c:\a.zip
Author: me
ChangeDescription:
PGROUP: A
PGROUP: B
PGROUP: C
Label: SOFTWARE
Label: READY
Name: 1.0
EntityPath: c:\a.zip
Author: me
ChangeDescription:
PGROUP: A
Label: GAME
Label: READY您可能需要对其进行一些调整,以获得所需的输出,但这将给您提供基本的想法。
发布于 2014-05-02 16:08:53
基于alecxe解决方案,我能够让它使用:
from xml.etree import ElementTree
try:
tree = ElementTree.parse(self.xml)
info_list = []
for revision in tree.findall('Revision'):
for key, value in revision.attrib.iteritems():
values = dict()
values[key] = value
info_list.append(values)
#print "%s: %s" % (key, value)
for child in revision:
values = dict()
# this is needed to match the change description field.
if child.tag == 'ChangeDescription':
values[child.tag] = child.text
#print "%s: %s" % (child.tag, child.text)
else:
values[child.tag] = child.attrib.get('Name', '')
#print "%s: %s" % (child.tag, child.attrib.get('Name', ''))
info_list.append(values)
print
for i in info_list:
print(i)
except Exception:
raise
finally:
self.xml = Nonehttps://stackoverflow.com/questions/23431656
复制相似问题