0

How can I obtain the values inside an element which has entitites and values?

The element are inside "level" Below are sample xml :

<observations>

<station wmo-id="94576" bom-id="040913" tz="Australia/Brisbane" stn- 
name="BRISBANE" stn-height="8.13" type="AWS" lat="-27.4808" lon="153.0389" 
forecast-district-id="QLD_PW015" description="Brisbane">

<period index="0" time-utc="2018-03-29T05:50:00+00:00" time-local="2018-03-                    
29T15:50:00+10:00" wind-src="OMD">

<level index="0" type="surface">
<element units="Celsius" type="apparent_temp">29.0</element>
<element units="Celsius" type="delta_t">2.1</element>
<element units="km/h" type="gust_kmh">11</element>
<element units="knots" type="wind_gust_spd">6</element>
<element units="Celsius" type="air_temperature">25.4</element>
<element start-time-local="2018-03-29T09:00:00+10:00" end-time-local="2018- 
03-29T15:51:00+10:00" duration="411" start-time-utc="2018-03- 
28T23:00:00+00:00" end-time-utc="2018-03-29T05:51:00+00:00" units="mm" 
type="rainfall">0.8</element>
</level>
</period>
</station>
</observations>

I have tried using for loop which able to return results up to

for station in root[1]:
  stnname = station.attrib['stn-name']
  for perioddata in station:
    index = perioddata.attrib['index']
    for leveldata in perioddata:
        typedata = leveldata.attrib['type']
        for elementdata in leveldata:
            value = elementdata.attrib['type']
            print(value)
            #result shows only type but not type's value
            #the result i want is apparent_temp & 29.0
Derek Lee
  • 475
  • 1
  • 6
  • 20
  • 1
    Possible duplicate of [Get list of XML attribute values in Python](https://stackoverflow.com/questions/87317/get-list-of-xml-attribute-values-in-python) – Narendra Mar 31 '18 at 03:49
  • the list above is using xpath, while i want to use elementtree – Derek Lee Mar 31 '18 at 03:55

1 Answers1

0

Given this element:

<element units="Celsius" type="apparent_temp">29.0</element>

units and type are attributes of the element, but 29.0 is the text of the element.

John Gordon
  • 29,573
  • 7
  • 33
  • 58
  • so how can i obtain the element text? i used elementdata.attrib['type'] returns "apparent _temp", but how can i obtain the element text? – Derek Lee Mar 31 '18 at 03:51
  • `elementdata.text` – John Gordon Mar 31 '18 at 03:51
  • i tried elementdata.text['apparent_temp'] and elementdata.text['type'] . i get TypeError: string indices must be integers, not str – Derek Lee Mar 31 '18 at 03:54
  • Just `elementdata.text` by itself. – John Gordon Mar 31 '18 at 03:57
  • Thanks, now i am able to get the data, however, if I want to obtain only attribute "gust_kmh" text, how can I do so? – Derek Lee Mar 31 '18 at 03:59
  • You'd have to loop through all the `` items, look for one that has a `gust_kmh` attribute, and get the element's text: `if 'gust_kmh' in elementdata.attrib: print (elementdata.text)` – John Gordon Mar 31 '18 at 04:01
  • Thanks. now i am able to get individual attrib's data. Just 1 more question, I am trying to get "start-time-local" values, but when i am using the same elementdata.text , i can't seems to get the 'start-time-local' values, any reason or mistake i'm doing? – Derek Lee Mar 31 '18 at 04:27
  • `start-time-local` is an _attribute_. It's in `elementdata.attrib`, not `elementdata.text`. – John Gordon Mar 31 '18 at 04:35
  • when i tried `if 'apparent_temp' in elementdata.attrib['type']: print(elementdata.text) ,` this works, however, if i try `if 'start-time-local' in elementdata.attrib: print(elementdata.text)` this doesn't work – Derek Lee Mar 31 '18 at 04:49
  • What do you mean "it doesn't work?" What did it do? Did you get an error? Did it not print anything? – John Gordon Mar 31 '18 at 23:44
  • Hi John, I have managed to get it run. Thanks alot for your help! – Derek Lee Apr 02 '18 at 21:21