Android XML Multi-Node Reading Using Current Date

ぐ巨炮叔叔 提交于 2019-12-25 02:55:12

问题


I'm creating a weather app which gets the min/max temperature from a 5 day forecast XML with same node names. I want to use the current date to look through the XML and find the correct min/max for that day.

This is the weather XML: Link

Here is my code, I've trimmed it just enough to the part where I don't understand the multi-nodes, but still I wanted it to be reusable (Currently it just gets the first min/max as denoted by a 0):

  public class MyAsyncTask extends AsyncTask < Void, Void, String > {
    //========================== pre execute to get date for xml =======
    protected void onPreExecute() {
      try {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
      } catch (Exception e) {}
    }@
    Override
    protected String doInBackground(Void...params) {

        //=========================== Load data using xml ================
        try {
          URL xmlUrl2 = new URL("http://api.openweathermap.org/data/2.5/forecast/daily?q=london&mode=xml&units=metric&cnt=5");

          InputStream inm = xmlUrl2.openStream();
          Document docm = parsem(inm);
          docm.getDocumentElement().normalize();

          Node nNodem = docm.getElementsByTagName("temperature").item(0);

          Element eElementm = (Element) nNodem;

          double dmax = Math.round(Double.parseDouble(eElementm.getAttribute("max")));
          int dxmax = (int) dmax;
          xmaxtemp = Integer.toString(dxmax);
          double dmin = Math.round(Double.parseDouble(eElementm.getAttribute("min")));
          int dxmin = (int) dmin;
          xmintemp = Integer.toString(dxmin);
        } catch (UnknownHostException s) {
          internet = false;
        } catch (IOException i) {
          System.out.println("IO Exception error!");
        } catch (Exception ex) {
          ex.printStackTrace();
        }
        return xtemp;
      }
      //========================= show data===============
      @
    Override
    protected void onPostExecute(String result) {
        TextView minmax = (TextView) findViewById(R.id.minmax);
        minmax.setText("↑" + xmaxtemp + "     " + xmintemp + "↓");
      }
      //======================== parse document =======
    public static Document parse(InputStream is) {
      Document ret = null;
      DocumentBuilderFactory domFactory;
      DocumentBuilder builder;

      try {
        domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setValidating(false);
        domFactory.setNamespaceAware(false);
        builder = domFactory.newDocumentBuilder();

        ret = builder.parse(is);
      } catch (Exception ex) {
        System.err.println("unable to load XML: " + ex);
      }
      return ret;
    }
  }

回答1:


For better use, u should use xpath to have a perfect manipulation over ur xml data:

this is an example how to get all temperature nodes

String expression = "//temperature";
NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);

After that you can manipulate the list.

this is a good tuto to start using xpath with java: java-xml-xpath-tutorial/



来源:https://stackoverflow.com/questions/29326601/android-xml-multi-node-reading-using-current-date

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!