Python 使用XML.etree解析XML(仅限)
使用xml.etree(请使用此模块) 我如何解析:Python 使用XML.etree解析XML(仅限),python,xml,python-2.7,xml.etree,Python,Xml,Python 2.7,Xml.etree,使用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">
<?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>
到目前为止,使用以下代码,我只能获得修订行,但我很难解析其他子字段:
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
查找所有
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
您可能需要对其进行一些调整,以获得所需的输出,但这应该为您提供了基本的想法。基于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 = None
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
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 = None