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 6571309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:51:57+00:00 2026-05-25T14:51:57+00:00

So im trying to parse and xml file in android. Bellow is a link

  • 0

So im trying to parse and xml file in android. Bellow is a link to the file and my code. I’m trying to get the current temp stored as “temp_f” in the “current_conditions” element. But every time i run it i get this error, 09-15 20:26:47.359: DEBUG/ErOr(17663): java.lang.ClassCastException: org.apache.harmony.xml.dom.ElementImpl

http://www.google.com/ig/api?weather=10598

DocumentBuilderFactory factory1 = DocumentBuilderFactory.newInstance();
            factory1.setNamespaceAware(true); // never forget this!
            InputSource source = new InputSource(new StringReader(xmltemp));
            Document doc = factory1.newDocumentBuilder().parse(source);

            String newstring = "" + ((DocumentBuilderFactory) ((Document) doc.getDocumentElement().
               getElementsByTagName("current_conditions").item(0)).
               getElementsByTagName("temp_f").item(0)).
               getAttribute("data");
  • 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-05-25T14:51:58+00:00Added an answer on May 25, 2026 at 2:51 pm

    Have a look at the Java XPath API — that should be much nicer than traversing the DOM to find your data:

    URL url = new URL("http://www.google.com/ig/api?weather=10598");
    InputSource xml = new InputSource(url.openStream());
    XPath xpath = XPathFactory.newInstance().newXPath();
    String data = xpath.evaluate("//current_conditions/temp_f/@data", xml);
    

    As for the original question:

    First put every statement of your extraction code on a line of its own, this will help you a lot with finding the problems (and line numbers in stacktrace will be a lot more precise):

    Element docElem = doc.getDocumentElement();
    Document curr_cond = (Document) /*1*/ docElem.getElementsByTagName("current_conditions").item(0);
    DocumentBuilderFactory temp_f = (DocumentBuilderFactory) /*2*/ curr_cond.getElementsByTagName("temp_f").item(0);
    String newstring = "" + temp_f.getAttribute("data");
    

    At /*1*/ you try to cast a org.w3.dom.Node into an org.w3.dom.Document — but not every Node is a Document, as can be seen by the ClassCastException here: The returned Node is actually an instance of the org.w3.dom.Element interface, more precisely an instance of class of org.apache.harmony.xml.dom.ElementImpl (part of the Apache Harmony XML parser implementation).

    So let’s remove the cast and change the declared type of curr_cond to Node:

    Element docElem = doc.getDocumentElement();
    Node curr_cond = docElem.getElementsByTagName("current_conditions").item(0);
    DocumentBuilderFactory temp_f = (DocumentBuilderFactory) /*2*/ curr_cond.getElementsByTagName("temp_f").item(0);
    String newstring = "" + temp_f.getAttribute("data");
    

    Now a second problem becomes obvious at /*2*/: The Node interface does not specify a getElementsByTagName() method, that’s probably why you did try to cast to Document above. This won’t compile and there is no way to get it to.

    Why you cast the result to DocumentBuilderFactory is beyond me. Yes, there is a getAttribute() method but both the class name and method javadoc should have made clear that it’s definitely not doing want you want.

    So my above recommendation holds: Please use XPath, it’s the better tool for this task 🙂


    There actually is a way to extract the data using only DOM for your first task (current temperature). It relies on the fact that the temp_f element is unique and is a lot more verbose than the above XPath expression, though:

    Element docElem = doc.getDocumentElement();
    Node temp_f = docElem.getElementsByTagName("temp_f").item(0);
    NamedNodeMap attributes = temp_f.getAttributes();
    Node dataAttr = attributes.getNamedItem("data");
    String data = dataAttr.getNodeValue();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get xml file from url link . This code for
I am trying to parse xml file and get no errors but when trying
I'm trying to parse an xml file that contains accents, but I get this
I'm trying to parse an XML file with Java and SAX for an android
I'm trying to parse a large file in android.The xml file size exceeds 2Mb.
I am trying to parse an xml file using SAX with Android and the
I am trying to parse one XML file that contains some unicode characters.I tried
I am trying to parse an xml file using XmlReader but although I am
I'm trying to parse an xml file to return a item with a specific
I'm trying to parse an xml file using lxml. xml.etree allowed me to simply

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.