Reputation: 45
I have an XML retrieved from NOAA and I am trying to parse it using minidom in Python, but I am not able to retrieve the values.
`<parameters applicable-location="point1">
<temperature type="maximum" units="Fahrenheit" time-layout="k-p24h-n7-1">
<name>Daily Maximum Temperature</name>
<value>75</value>
<value>67</value>
<value>65</value>
<value>72</value>
<value>65</value>
<value>64</value>
<value>62</value>
</temperature>
</parameters>
`
I need to retrieve the values under tag maximum temperature.
Upvotes: 0
Views: 714
Reputation: 5454
You can use Beautiful Soup with libxml. Here is how to do proper setup tested for ubuntu 14.04:
sudo apt-get install libxml2-dev libxslt1-dev lib32z1-dev python-dev -y
pip install lxml
pip install beautifulsoup4
Replace python-dev
with python3-dev
if you are using python3. You can parse xml as follows:
file_content = """your xml string here"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(file_content, "xml")
max_temp_list = [int(item.string) for item in soup.find("temperature", {"type": "maximum"}).findAll("value")]
print(max_temp_list)
Please refer to documentation for further examples of finding elements.
Upvotes: 2
Reputation: 615
Using the BeautifulpSoup is an easy way.
You can try. like this.
from bs4 import BeautifulSoup
XML_STRING = """
<parameters applicable-location="point1">
<temperature type="maximum" units="Fahrenheit" time-layout="k-p24h-n7-1">
<name>Daily Maximum Temperature</name>
<value>75</value>
<value>67</value>
<value>65</value>
<value>72</value>
<value>65</value>
<value>64</value>
<value>62</value>
</temperature>
</parameters>
"""
soup = BeautifulSoup(XML_STRING, 'html.parser')
for tag in soup.find_all('value'):
print(tag.string)
Upvotes: 3