frank hk
frank hk

Reputation: 127

Parsing XML file using element tree python3

I want to parse an XML file fragment given below to extract the viewpoint tag and its attribute names. I also want to create a table to tabulate the extracted data.

My XML file fragment:

 <windows source-height='51'>
        <window class='dashboard' maximized='true' name='Figure 8-59'>
          <viewpoints>
            <viewpoint name='Good Filter Design'>
              <zoom type='entire-view' />
              <geo-search-visibility value='1' />
            </viewpoint>
            <viewpoint name='Poor Filter Design'>
              <zoom type='entire-view' />
              <geo-search-visibility value='1' />
            </viewpoint>
          </viewpoints>
          <active id='-1' />
        </window>
        <window class='dashboard' name='Figure 8-60 thought 8-65'>
          <viewpoints>
            <viewpoint name='Heat Map'>
              <zoom type='entire-view' />
              <geo-search-visibility value='1' />
            </viewpoint>
            <viewpoint name='Lightbulb'>
              <zoom type='entire-view' />
              <geo-search-visibility value='1' />
            </viewpoint>
            <viewpoint name='Sales Histogram'>
              <zoom type='entire-view' />
              <geo-search-visibility value='1' />
            </viewpoint>
          </viewpoints>
          <active id='-1' />
        </window>
</windows>

I want to extract and keep the "good filter design" and "poor filter design" in one row and the remaining three view point names as a second row.

My attempt:

root = getroot('example.xml')
for i in root.findall('windows/window/viewpoints/viewpoint'):
    print(i.get('name'))

Upvotes: 1

Views: 1028

Answers (2)

har07
har07

Reputation: 89285

Using elementtree should be as easy. I don't know what getroot() do exactly, but if it really return root element of the XML document, then you shouldn't mention window in the findall parameter :

>>> from xml.etree import ElementTree as ET
>>> raw = '''your XML string'''
>>> root = ET.fromstring(raw)
>>> for v in root.findall('window/viewpoints'):
...     print([a.get('name') for a in v.findall('viewpoint')])
... 
['Good Filter Design', 'Poor Filter Design']
['Heat Map', 'Lightbulb', 'Sales Histogram']

demo

Upvotes: 1

Rahul
Rahul

Reputation: 11520

If you can use beautifulsoup thismuch easy it is

from bs4 import BeautifulSoup
#xml = """your xml"""
soup = BeautifulSoup(xml, 'lxml')
names = [viewpt["name"] for viewpt in soup.find_all('viewpoint')]

This will give every tag named 'viewpoint'

If you only want nested one use this:

names = [viewpoint["name"]
        for windows in soup.find_all('windows')
            for window in windows.find_all("window")
                for viewpoints in window.find_all("viewpoints")
                    for viewpoint in viewpoints.find_all("viewpoint")]

in your case both will give:

Out[18]: 
['Good Filter Design',
 'Poor Filter Design',
 'Heat Map',
 'Lightbulb',
 'Sales Histogram']

Upvotes: 0

Related Questions