使用 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
到目前为止,使用以下代码我只能获得 Revision 行,但我正在努力解析其他子字段:
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