Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!bloom-beacon.mit.edu!bloom-beacon.mit.edu!panix!gordon From: John Gordon Newsgroups: comp.lang.python Subject: Re: Parsing XML Using XPATH for Python Date: Tue, 2 Dec 2014 22:52:27 +0000 (UTC) Organization: PANIX Public Access Internet and UNIX, NYC Lines: 80 Message-ID: References: NNTP-Posting-Host: panix2.panix.com X-Trace: reader1.panix.com 1417560747 8461 166.84.1.2 (2 Dec 2014 22:52:27 GMT) X-Complaints-To: abuse@panix.com NNTP-Posting-Date: Tue, 2 Dec 2014 22:52:27 +0000 (UTC) User-Agent: nn/6.7.3 Xref: csiph.com comp.lang.python:81819 In Uzoma Ojemeni writes: > I am new to Python - a few days old - and I would appreciate some help. > I want write a python code to parse the below XML as below:- > ServingCell----------NeighbourCell > L41_NBR3347_1----------L41_NBR3347_2 > L41_NBR3347_1----------L41_NBR3347_3 > L41_NBR3347_1----------L41_NBR3349_1 > L41_NBR3347_1----------L41_NBREA2242_1 > > > > 1 > > > > lDot0 lDot0 > > > > > > > > > > > > > > > > > In plain English, it looks like you want to do this: 1. Print a header. 2. For each element: 3. Find the child element. 4. For each child element: 5. Print the "id" attributes of the element and the element. Translated to python, that would look something like this: # import the xml library code import xml.etree.ElementTree as ET # load your XML file tree = ET.parse('cells.xml') # get the root element root = tree.getroot() # print a header print("ServingCell------NeighbourCell") # find each child element for serving_cell in root.findall('LteCell'): # find the child element attributes = serving_cell.find('attributes') # find each child element for neighbor in attributes.findall('LteNeighboringCellRelation'): # print the id's of the serving and neighbor cells print("%s------%s" % (serving_cell.attrib['id'], neighbor.attrib['id'])) -- John Gordon Imagine what it must be like for a real medical doctor to gordon@panix.com watch 'House', or a real serial killer to watch 'Dexter'.