Android的XML多节点阅读使用当前日期 [英] Android XML Multi-Node Reading Using Current Date
问题描述
我创建一个天气应用程序从5天的天气预报XML具有相同的节点名称获取最小/最大温度。我想用当前日期通过XML看,发现这一天的正确的最小/最大。
这是天气的XML:<一href=\"http://api.openweathermap.org/data/2.5/forecast/daily?q=london&mode=xml&units=metric&cnt=5\"相对=nofollow>链接
下面是我的code,我修剪它刚好在那里我不明白多节点的一部分,但我仍然希望它是可重复使用(目前它只是获取第一个最小/最大如通过表示一个0):
公共类MyAsyncTask扩展的AsyncTask&LT;虚空,虚空,字符串&GT; {\r
// ========================== pre执行获取日期为XML =======\r
在preExecute保护无效(){\r
尝试{\r
日期格式DF =新的SimpleDateFormat(YYYY-MM-DD);\r
}赶上(例外五){}\r
} @\r
覆盖\r
保护字符串doInBackground(虚空...... PARAMS){\r
\r
使用XML // ===========================加载数据================\r
尝试{\r
网址xmlUrl2 =新URL(\"http://api.openweathermap.org/data/2.5/forecast/daily?q=london&mode=xml&units=metric&cnt=5\");\r
\r
InputStream的INM = xmlUrl2.openStream();\r
文档DOCM = parsem(INM);\r
docm.getDocumentElement()正常化()。\r
\r
。节点nNodem = docm.getElementsByTagName(温度)项目(0);\r
\r
元素eElementm =(元)nNodem;\r
\r
双DMAX = Math.round(Double.parseDouble(eElementm.getAttribute(MAX)));\r
INT dxmax =(int)的DMAX;\r
xmaxtemp = Integer.toString(dxmax);\r
双DMIN = Math.round(Double.parseDouble(eElementm.getAttribute(分)));\r
INT dxmin =(INT)DMIN;\r
xmintemp = Integer.toString(dxmin);\r
}赶上(的UnknownHostException S){\r
互联网= FALSE;\r
}赶上(IOException异常I){\r
的System.out.println(IO异常错误!);\r
}赶上(例外前){\r
ex.printStackTrace();\r
}\r
返回xtemp;\r
}\r
// =========================的数据显示===============\r
@\r
覆盖\r
保护无效onPostExecute(字符串结果){\r
TextView的MINMAX =(的TextView)findViewById(R.id.minmax);\r
minmax.setText(↑+ xmaxtemp ++ xmintemp +↓);\r
}\r
// ========================解析文档=======\r
公共静态文档解析(InputStream为){\r
文档RET = NULL;\r
的DocumentBuilderFactory domFactory;\r
的DocumentBuilder建设者;\r
\r
尝试{\r
domFactory = DocumentBuilderFactory.newInstance();\r
domFactory.setValidating(假);\r
domFactory.setNamespaceAware(假);\r
建设者= domFactory.newDocumentBuilder();\r
\r
RET = builder.parse(是);\r
}赶上(例外前){\r
通信System.err.println(无法加载XML:+ EX);\r
}\r
返回RET;\r
}\r
}
\r
有关更好地使用,U应使用XPath拥有完美地操控UR XML数据:
这是一个例子,如何让所有的温度节点
字符串前pression =//温度;
节点列表节点列表=(节点列表)xPath.compile(如pression)评估(XMLDOCUMENT,XPathConstants.NODESET);
在可以操作的列表中。
这是一个很好的政党成员开始使用XPath与Java:
Java的XML-XPath的教程/
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;
}
}
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/
这篇关于Android的XML多节点阅读使用当前日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!