0

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.

2
  • You could trim that example down to a smaller test set. But more importantly, you need to post the code you already have. We cannot suggest fixes to code that we cannot see. Commented May 2, 2017 at 4:23
  • Further to @tdelaney: read How to Ask and minimal reproducible example. Commented May 3, 2017 at 9:08

2 Answers 2

3

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)
Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.