iNoob
iNoob

Reputation: 1395

Parsing data with elementtree into a dictionary/list

I'm working with Elementtree to parse an XML file (Nessus data). Ive identified the item.attrib which looks to be a dictionary with a 'name': 'IPaddress'. I'd like to add this data into a dictionary, or if I can access just the ipaddress into a list. How can I access the value for name only? Ive tried using variations on item[0]/[1]/.attrib/text/ but still no luck.

Current Code

import elementtree.ElementTree as ET

def getDetails(nessus_file):
    host_list = []
    host_dict = {}
    try:
        tree = ET.parse(nessus_file)
        doc = tree.getroot()
        reporthost = doc.getiterator('ReportHost')
        for child in doc:
            if child.tag == 'Report':
                for item in child:
                    if item.tag == 'ReportHost':
                        print item.attrib

    except Exception as e:
        print e
        exit()
getDetails('file.nessus')

Example Output From Current Code

{'name': '172.121.26.80'}
{'name': '172.121.26.42'}
{'name': '172.121.26.41'}
{'name': '172.121.26.21'}
{'name': '172.121.26.15'}
{'name': '172.121.26.14'}

Upvotes: 1

Views: 1805

Answers (1)

Related Questions