Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7893609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:10:34+00:00 2026-06-03T07:10:34+00:00

Edit: 2020 update — Please disregard the question below as Google’s weather API is

  • 0

Edit: 2020 update — Please disregard the question below as Google’s weather API is no longer able to be used. If you are interested in using a weather API, Darksky is the defacto that most people use: https://darksky.net/dev

If you use Java, the normal way to parse the JSON is using org.json: https://devqa.io/java/how-to-parse-json-in-java

—

Original question below.


I’ve been playing around with xml documents and java a bit recently, but I’ve had absolutely no luck using the google weather API.

Let’s assume that I’m trying to do a simple object to store current temp, and forecast temp for just tomorrow, how would I do this?

http://www.google.com/ig/api?weather=02110 Is the working example for my home city.

Thanks

Using this code:

public static final String[] xmlLoader(){
    String xmlData[] = new String[2];
    try {
        URL googleWeatherXml = new URL("http://www.google.com/ig/api?weather=02110");
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();    
        Document doc = db.parse(googleWeatherXml.openStream());

         // normalize text representation
        doc.getDocumentElement ().normalize ();
        NodeList listOfWeek = doc.getElementsByTagName("");

            Node firstWeekNode = listOfWeek.item(dateCounter-1);
            int totalWeeks = listOfWeek.getLength();


            //Break xml file into parts, then break those parts down int an array by passing individual elements to srtings
            if(firstWeekNode.getNodeType() == Node.ELEMENT_NODE){
                Element firstWeekElement = (Element)firstWeekNode;
                //-------
                NodeList dateList = firstWeekElement.getElementsByTagName("date");
                Element dateElement = (Element)dateList.item(0);

                NodeList textDateList = dateElement.getChildNodes();
                xmlData[0]= (((Node)textDateList.item(0)).getNodeValue().trim()).toString();

                //-------
                NodeList riddleList = firstWeekElement.getElementsByTagName("riddle");
                Element riddleElement = (Element)riddleList.item(0);

                NodeList textRiddleList = riddleElement.getChildNodes();
                xmlData[1]= (((Node)textRiddleList.item(0)).getNodeValue().trim()).toString();

                //----
                NodeList lWSList = firstWeekElement.getElementsByTagName("lastWeekSolution");
                Element ageElement = (Element)lWSList.item(0);

                NodeList textLWSList = ageElement.getChildNodes();
                xmlData[2]= (((Node)textLWSList.item(0)).getNodeValue().trim()).toString();

                //------
            }//end of if clause

    }
    catch(MalformedURLException f){System.err.println(f.getMessage()); }
    catch(NullPointerException npe){
        System.out.println("The Weather Data you searched for is incorrect or does not yet exist, try again. ");
        String s[] = {" ", " "};
        main(s);
    }

   catch (SAXParseException err) {
    System.out.println ("** Parsing error" + ", line " 
         + err.getLineNumber () + ", uri " + err.getSystemId ());
    System.out.println(" " + err.getMessage ());

    }
    catch (SAXException e) {
    Exception x = e.getException ();
    ((x == null) ? e : x).printStackTrace ();

    }catch (Throwable t) {
    t.printStackTrace ();
    }
    return xmlData;
}

Getting tons of null pointers, no matter what I do.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T07:10:36+00:00Added an answer on June 3, 2026 at 7:10 am

    When you call .getElementsByTagName(), you need to put a guard on the value you get back:

    NodeList dateList = firstWeekElement.getElementsByTagName("date");
    if (datelist != null) {
        Element dateElement = (Element)dateList.item(0);
    

    et cetera.

    Update

    Now that I look at the XML, I see that there are no <date> elements; so that’s why dateList is null. You want to be looking for elements like <forecast_information> and <city>.

    NodeList forecasts = firstWeekElement.getElementsByTagName("forecast_information");
    if (forecasts != null) {
        Element forecastElement = (Element)forecasts.item(0);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Edit: The below question was answered by this . I have a new updated
EDIT: It looks like I'm completely misinformed. Please close this thread. Gah. For the
Edit: With enough rewriting, and commenting, I have it running, will post final below
Edit: I'm looking for solution for this question now also with other programming languages.
EDIT: My question was deficient because it was misunderstood. I'm rephrasing it. The original
[Edit: see final code below] I'm using the code below to randomly select 5
[Edit: see below for final code] I have the following code and I'm trying
Edit: This question was written in 2008, which was like 3 internet ages ago.
Edit: From another question I provided an answer that has links to a lot
EDIT: As it turns out when I was browsing I found a question the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.