在我的XML文件中,我有一些Windows标签,我想从中根据适当的窗口名称逐一提取视点的名称。
我们是CML鱼片:
<windows>
<window class='dashboard' maximized='true' name='Dashboard 1'>
<viewpoints>
<viewpoint name='Category sheet'>
<zoom type='entire-view' />
</viewpoint>
<viewpoint name='Segment Sheet'>
<zoom type='entire-view' />
<selection-collection>
<tuple-selection>
<tuple-reference>
<tuple-descriptor>
<pane-descriptor>
<x-fields>
<field>[federated.1y3sjvb0pyupci132wn6b0wdgpc3].[none:Segment:nk]</field>
</x-fields>
<y-fields>
<field>[federated.1y3sjvb0pyupci132wn6b0wdgpc3].[sum:Sales:qk]</field>
</y-fields>
</pane-descriptor>
<columns>
<field>[federated.1y3sjvb0pyupci132wn6b0wdgpc3].[none:Segment:nk]</field>
<field>[federated.1y3sjvb0pyupci132wn6b0wdgpc3].[sum:Sales:qk]</field>
</columns>
</tuple-descriptor>
<tuple>
<value>"Corporate"</value>
<value>706146.36680000008</value>
</tuple>
</tuple-reference>
</tuple-selection>
</selection-collection>
</viewpoint>
<viewpoint name='Subcat Sheet'>
<zoom type='entire-view' />
</viewpoint>
<viewpoint name='sub cat grp Sheet'>
<zoom type='entire-view' />
</viewpoint>
</viewpoints>
<active id='4' />
</window>
<window class='dashboard' name='Story 1'>
<viewpoints />
<active id='4' />
</window>
</windows>
我的尝试:
for win in root.findall('./windows/window'):
dashwins = win.find('window[@class="dashboard"]')
if dashwins != None:
print(dashwins.attrib)
for i in dashwins:
view1 = i.find('viewpoint')
for j in view1:
print(j.get('name'))
您可以扩展xpath表达式以获取目标viewpoint
元素下的所有window
元素,然后询问它们的name
属性。
>>> from xml.etree import ElementTree
>>> tree = ElementTree.parse('sanu.xml')
>>> for el in tree.findall('.//window[@class="dashboard"]//viewpoint'):
... el.attrib['name']
...
'Category sheet'
'Segment Sheet'
'Subcat Sheet'
'sub cat grp Sheet'