0

In my XML file, i have some windows tags from which I want to extract the name of the viewpoint one by one under appropriate window name.

My XML file:

<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>&quot;Corporate&quot;</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>

MY try :

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'))
  • Viewpoints name need to be extracted if and only if the class=dashboard attribute present inside window tag.

1 Answer 1

1

You can extend your xpath expression to get all of the viewpoint elements under the target window elements, then ask for their name attributes.

>>> 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'
Sign up to request clarification or add additional context in comments.

4 Comments

Thankyou @Bill Bell
Now i executed the code you recommended, But Control is not going inside the for-loop.
hope you will respond for this @Bill Bell
We are in very different time zones. (I live in Canada.) Would you please explain what you mean by 'control is not going inside the for-loop.'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.