Python: access the value of a nested children in WaterML (xml) file with ElementTree

igarousi

I want to access the values of a child in a WaterML file.

In this WaterML file, a child called values has several children as value. For example, the first element in the values has the variable value of 506.351. The problem is that I cannot access this value when using the below code in Python.

import requests
import xml.etree.ElementTree as ET


url = https://hs-apps.hydroshare.org/apps/nwm-forecasts/api/GetWaterML/?archive=rolling&config=analysis_assim&geom=forcing&variable=SWDOWN&COMID=2236,1144&lon=-98&lat=38.5&startDate=2018-07-23&endDate=2018-07-25&time=00&lag=t00z

r = requests.get(url)
root = ET.fromstring(r.content)
for child in root.iter('{http://www.cuahsi.org/waterML/1.1/}value'):
    print(child.tag, child.attrib)

The output does not include the variable value (for example, 506.351)

I do appreciate any help or idea on how to access the variable values in this file.

JY Hsiao

You can use child.text to extract text surrounded by value tag.

import requests
import xml.etree.ElementTree as ET

url = 'https://hs-apps.hydroshare.org/apps/nwm-forecasts/api/GetWaterML/?archive=rolling&config=analysis_assim&geom=forcing&variable=SWDOWN&COMID=2236,1144&lon=-98&lat=38.5&startDate=2018-07-23&endDate=2018-07-25&time=00&lag=t00z'

requests.get(url)
root = ET.fromstring(r.content)
for child in root.iter('{http://www.cuahsi.org/waterML/1.1/}value'):
    print(child.tag, child.attrib, child.text, '\n')

The output is:

{http://www.cuahsi.org/waterML/1.1/}value {'censorCode': 'nc', 'dateTime': '2018-07-23T00:00:00', 'timeOffset': '0', 'dateTimeUTC': '2018-07-23T00:00:00', 'methodCode': '1', 'sourceCode': '1', 'qualityControlLevelCode': '1'} 506.351 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Access nested children in xml file parsed with ElementTree

Dump elementtree into xml file

Check if XML Element has children or not, in ElementTree

Access element and tag in xml file using ElementTree

How to access attribute value in xml containing namespace using ElementTree in python

Parsing XML using Python ElementTree

Python 3 How to search children nodes of xml.etree.ElementTree for strings in a list

How to access nested children with same name in XML using lxml in python

How do I search for a Tag in xml file using ElementTree where i have a certain "Parent"tag with a specific value? (python)

Python access nested Dictionary Value

XML Parsing Python ElementTree - Nested for loops

Python write result of XML ElementTree findall to a file

Parse nested XML file and get the value of previous tag in python

Python 2.5: ElementTree and UML in XML

Using Python ElementTree/ElementInclude and xpointer to access included XML files

Modify XML file using ElementTree

Parsing XML in Python with ElementTree - findall

Access children value jquery

Checking for the existence of children when using ElementTree for XML parsing in python

Python XML PArse with ElementTree

Python XML ElementTree - findall

Python write multiple xml structures in one xml file with ElementTree

Python - Access nested enum value

XML Parsing with Python - find attribute value with Elementtree

Get element value by name from xml file using Python xml.etree.ElementTree

Parse XML file in python and retrieve nested children

python, ElementTree, Issue when adding and removing subelements from xml file

Python XML ElementTree not reading node with &

Get xml value of ElementTree Element