How to read deeper into XML

edited July 2016 in Questions about Code

Hello, I've been able to follow examples of the loadXML example on this page https://processing.org/reference/XML.html successfully, but I am trying to work with an XML file from wunderground.com weather API that looks like this:

api.wunderground.com/api/5a7d1edd0fd022d0/forecast/q/TN/gatlinburg.xml

Out of this whole thing, I was hoping to just isolate a few elements like HIGH and LOW temperatures and storm warnings. I just can't figure out how to jump to those tags.

Using a different XML from the same API I was able to do what I want with the following code, but the above code has more "Layers".

This is pretty much what I was trying to do:

                    XML xmlFeed = loadXML("weather.xml");

                    int numSites = xmlFeed.getChildCount();     
                    //println(numSites);
                    for (int i = 0; i < numSites; i++) {   
                      XML tag = xmlFeed.getChild(i);
                      if (tag.getName().equals("temperature_string")) {
                        String temperature_string = tag.getContent();
                        println("temperature_string: " + temperature_string);
                      } else if (tag.getName().equals("dewpoint_f")) {
                        String dewpoint_f = tag.getContent();
                        println("dewpoint_f: " + dewpoint_f);
                      }
                    }
                    exit();

I very much appreciate any help anyone is able to give.

Answers

  • edited July 2016

    Thanks GoToLoop, for all the time you spend helping people. I will try working off the code you referenced when I get home from work tonight and let you know how it goes.

  • edited July 2016

    Ok I tried adjusting one of your previous snippets to this:

        XML xml;
        String[] forecast;
        xml = loadXML("weather.xml");
    
        XML[] forecastdays = xml.getChild("forecastdays").getChildren("forecastday");
    
        forecast = new String[forecastdays.length];
        int idx = 0;
    
        for (XML element : forecastdays)  forecast[idx++] = element
          .getChild("title")
          .getContent();
    
        printArray(forecast);
        exit();
    

    but now I'm getting a NullPointerException on line 5.

  • edited July 2016

    OK! finally figured out how to do what I wanted with the help of GoToLoop and some fiddling. Here's my code to help others (and please let me know if there are better ways to do this):

            XML xml;
            String[] titles;
            String[] pop;
    
            xml = loadXML("weather.xml");
    
            XML[] forecastdays = xml.getChild("forecast").getChild("txt_forecast").getChild("forecastdays").getChildren("forecastday");
    
            titles = new String[forecastdays.length];
            pop = new String[forecastdays.length];
            int idx = 0;
            for (XML element : forecastdays)  titles[idx++] = element
              .getChild("title")
              .getContent();
    
            idx = 0;
            for (XML element : forecastdays)  pop[idx++] = element
              .getChild("pop")
              .getContent();
    
            for (int day = 0; day < forecastdays.length; day++){
              println("On "+ titles[day] + ", there is a " + pop[day] + "% chance of rain.");  
            }
    

    and this prints:

    On Friday, there is a 40% chance of rain.
    On Friday Night, there is a 50% chance of rain.
    On Saturday, there is a 50% chance of rain.
    On Saturday Night, there is a 50% chance of rain.
    On Sunday, there is a 60% chance of rain.
    On Sunday Night, there is a 60% chance of rain.
    On Monday, there is a 60% chance of rain.
    On Monday Night, there is a 20% chance of rain.
    

    Thanks again for your help GoToLoop

  • edited July 2016 Answer ✓

    And please let me know if there are better ways to do this.

    Very good, congratz! =D> Not better but just slightly diff. below: :)

    /**
     * XML Forecasts (v2.0)
     * by  JamesS (2016-Jul-30)
     * mod GoToLoop
     * forum.Processing.org/two/discussion/17692/how-to-read-deeper-into-xml
     */
    
    static final String HTTP = "HTTP://";
    static final String SITE = "api.WunderGround.com";
    static final String FOLDERS = "/api/5a7d1edd0fd022d0/forecast/q/TN/";
    static final String XML_FILE = "gatlinburg.xml";
    
    String[] weeks;
    int[] rains;
    
    void setup() {
      XML xml = loadXML(HTTP + SITE + FOLDERS + XML_FILE);
    
      XML[] casts = xml.getChild("forecast").getChild("txt_forecast")
        .getChild("forecastdays").getChildren("forecastday");
    
      int len = casts.length;
    
      weeks = new String[len];
      rains = new int[len];
    
      for (int i = 0; i < len; ++i) {
        XML cast = casts[i];
    
        weeks[i] = cast.getChild("title").getContent();
        rains[i] = cast.getChild("pop").getIntContent();
      }
    
      for (int i = 0; i < len; ++i)
        println("On", weeks[i], "there is a", rains[i] + "% chance of rain.");
    
      exit();
    }
    
Sign In or Register to comment.