Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #81815 > unrolled thread

Parsing XML Using XPATH for Python

Started byUzoma Ojemeni <uojemeni@gmail.com>
First post2014-12-02 14:13 -0800
Last post2014-12-03 07:59 +0100
Articles 4 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Parsing XML Using XPATH for Python Uzoma Ojemeni <uojemeni@gmail.com> - 2014-12-02 14:13 -0800
    Re: Parsing XML Using XPATH for Python John Gordon <gordon@panix.com> - 2014-12-02 22:52 +0000
      Re: Parsing XML Using XPATH for Python Uzoma Ojemeni <uojemeni@gmail.com> - 2014-12-06 02:57 -0800
    Re: Parsing XML Using XPATH for Python dieter <dieter@handshake.de> - 2014-12-03 07:59 +0100

#81815 — Parsing XML Using XPATH for Python

FromUzoma Ojemeni <uojemeni@gmail.com>
Date2014-12-02 14:13 -0800
SubjectParsing XML Using XPATH for Python
Message-ID<a1a70942-6740-4de5-b41e-57a71fb36910@googlegroups.com>
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

<LteCell id="L41_NBR3347_1">
 <attributes>
  <absPatternInfoTdd><unset/></absPatternInfoTdd>
  <additionalSpectrumEmission>1</additionalSpectrumEmission>
  <additionalSpectrumEmissionList><unset/></additionalSpectrumEmissionList>
  <LteSpeedDependentConf id="0">                   
    <attributes>                                    
     <tReselectionEutraSfHigh>lDot0</tReselectionEut
     <tReselectionEutraSfMedium>lDot0</tReselectionE
    </attributes>                                   
   </LteSpeedDependentConf>                         
   <LteNeighboringCellRelation id="L41_NBR3347_2">  
    <attributes>                                    
     <absPatternInfo><unset/></absPatternInfo>      
   </LteNeighboringCellRelation>                    
   <LteNeighboringCellRelation id="L41_NBR3347_3">  
    <attributes>                                    
     <absPatternInfo><unset/></absPatternInfo>      
   </LteNeighboringCellRelation>                    
   <LteNeighboringCellRelation id="L41_NBR3349_1">  
    <attributes>                                    
     <absPatternInfo><unset/></absPatternInfo>                            
   </LteNeighboringCellRelation>                    
   <LteNeighboringCellRelation id="L41_NBREA2242_1">
    <attributes>                                    
     <absPatternInfo><unset/></absPatternInfo>      
     <absPatternInfoTdd><unset/></absPatternInfoTdd>

[toc] | [next] | [standalone]


#81819

FromJohn Gordon <gordon@panix.com>
Date2014-12-02 22:52 +0000
Message-ID<m5lfrb$88d$1@reader1.panix.com>
In reply to#81815
In <a1a70942-6740-4de5-b41e-57a71fb36910@googlegroups.com> Uzoma Ojemeni <uojemeni@gmail.com> 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

> <LteCell id="L41_NBR3347_1">
>  <attributes>
>   <absPatternInfoTdd><unset/></absPatternInfoTdd>
>   <additionalSpectrumEmission>1</additionalSpectrumEmission>
>   <additionalSpectrumEmissionList><unset/></additionalSpectrumEmissionList>
>   <LteSpeedDependentConf id="0">                   
>     <attributes>                                    
>      <tReselectionEutraSfHigh>lDot0</tReselectionEut
>      <tReselectionEutraSfMedium>lDot0</tReselectionE
>     </attributes>                                   
>    </LteSpeedDependentConf>                         
>    <LteNeighboringCellRelation id="L41_NBR3347_2">  
>     <attributes>                                    
>      <absPatternInfo><unset/></absPatternInfo>      
>    </LteNeighboringCellRelation>                    
>    <LteNeighboringCellRelation id="L41_NBR3347_3">  
>     <attributes>                                    
>      <absPatternInfo><unset/></absPatternInfo>      
>    </LteNeighboringCellRelation>                    
>    <LteNeighboringCellRelation id="L41_NBR3349_1">  
>     <attributes>                                    
>      <absPatternInfo><unset/></absPatternInfo>                            
>    </LteNeighboringCellRelation>                    
>    <LteNeighboringCellRelation id="L41_NBREA2242_1">
>     <attributes>                                    
>      <absPatternInfo><unset/></absPatternInfo>      
>      <absPatternInfoTdd><unset/></absPatternInfoTdd>

In plain English, it looks like you want to do this:

1. Print a header.
2. For each <LteCell> element:
3. Find the child <attributes> element.
4. For each child <LteNeighboringCellRelation> element:
5. Print the "id" attributes of the <LteCell> element and the
   <LteNeighboringCellRelation> 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 <LteCell> child element
for serving_cell in root.findall('LteCell'):

   # find the <attributes> child element
   attributes = serving_cell.find('attributes')

   # find each <LteNeighboringCellRelation> 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'.

[toc] | [prev] | [next] | [standalone]


#82038

FromUzoma Ojemeni <uojemeni@gmail.com>
Date2014-12-06 02:57 -0800
Message-ID<74befca0-422f-4a62-8ceb-0dba648fa591@googlegroups.com>
In reply to#81819
On Tuesday, December 2, 2014 11:52:37 PM UTC+1, John Gordon wrote:
> In <a1a70942-6740-4de5-b41e-57a71fb36910@googlegroups.com> Uzoma Ojemeni <uojemeni@gmail.com> 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
> 
> > <LteCell id="L41_NBR3347_1">
> >  <attributes>
> >   <absPatternInfoTdd><unset/></absPatternInfoTdd>
> >   <additionalSpectrumEmission>1</additionalSpectrumEmission>
> >   <additionalSpectrumEmissionList><unset/></additionalSpectrumEmissionList>
> >   <LteSpeedDependentConf id="0">                   
> >     <attributes>                                    
> >      <tReselectionEutraSfHigh>lDot0</tReselectionEut
> >      <tReselectionEutraSfMedium>lDot0</tReselectionE
> >     </attributes>                                   
> >    </LteSpeedDependentConf>                         
> >    <LteNeighboringCellRelation id="L41_NBR3347_2">  
> >     <attributes>                                    
> >      <absPatternInfo><unset/></absPatternInfo>      
> >    </LteNeighboringCellRelation>                    
> >    <LteNeighboringCellRelation id="L41_NBR3347_3">  
> >     <attributes>                                    
> >      <absPatternInfo><unset/></absPatternInfo>      
> >    </LteNeighboringCellRelation>                    
> >    <LteNeighboringCellRelation id="L41_NBR3349_1">  
> >     <attributes>                                    
> >      <absPatternInfo><unset/></absPatternInfo>                            
> >    </LteNeighboringCellRelation>                    
> >    <LteNeighboringCellRelation id="L41_NBREA2242_1">
> >     <attributes>                                    
> >      <absPatternInfo><unset/></absPatternInfo>      
> >      <absPatternInfoTdd><unset/></absPatternInfoTdd>
> 
> In plain English, it looks like you want to do this:
> 
> 1. Print a header.
> 2. For each <LteCell> element:
> 3. Find the child <attributes> element.
> 4. For each child <LteNeighboringCellRelation> element:
> 5. Print the "id" attributes of the <LteCell> element and the
>    <LteNeighboringCellRelation> 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 <LteCell> child element
> for serving_cell in root.findall('LteCell'):
> 
>    # find the <attributes> child element
>    attributes = serving_cell.find('attributes')
> 
>    # find each <LteNeighboringCellRelation> 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'.

Thank you Guys. I will implement and feedback if i find any problems.

[toc] | [prev] | [next] | [standalone]


#81842

Fromdieter <dieter@handshake.de>
Date2014-12-03 07:59 +0100
Message-ID<mailman.16517.1417589966.18130.python-list@python.org>
In reply to#81815
Uzoma Ojemeni <uojemeni@gmail.com> writes:
...
One easy option would be to use the "XPath" support in the "lxml"
package -- provided you have not problem with the installation
of external libraries ("libxml2" and "libxslt") and C-extensions ("lxml").

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web