关于XML解析 以前有说过 不过那是SAX方式的 今天说一下DOM方法
[序言]
1. 今天解析的目标是:香港天气rss 地址为:
- http://202.140.96.134:8080/FS-RSS/ftpfile/local_weather.xml
现在的目标就是:定制化该目标的解析办法 我们还是查看一下该地址的源文件 具体方法:
现在贴其源文件://注:为了阅读方便 我加了一些“回车换行”源文件是没有这些的
- <?xml version="1.0" encoding="utf-8"?>
- <rss version="2.0">
- <channel><title>HK Weather Today</title>
- <description>HK Weather Today</description>
- <item><title>HK Weather Today</title>
- <pubDate>Wed, 24 Mar 2010 07:00:00 GMT</pubDate>
- <description><![CDATA[Current Temperature = 22<br>Humidity = 96<br>UV Index = null<br>UV Intensity = null<br>Icon = SI<br><img src="img/SI.png" border="0"/> <br>]]> </description>
- </item>
- </channel></rss>
下面叙述具体怎么解析:
1. 定义界面 内有若干TextView 供显示结果用
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:id="@+id/city"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- <TextView
- android:id="@+id/time"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- <TextView
- android:id="@+id/temp"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- <TextView
- android:id="@+id/humidity"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- <TextView
- android:id="@+id/uvIndex"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- <TextView
- android:id="@+id/uvIntensity"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- <ImageView
- android:id="@+id/image"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
2. 得到各个View 的实例
- public void initialView(){
- city = (TextView)findViewById(R.id.city);
- time = (TextView)findViewById(R.id.time);
- temp = (TextView)findViewById(R.id.temp);
- hunidity = (TextView)findViewById(R.id.humidity);
- uvIndex = (TextView)findViewById(R.id.uvIndex);
- uvIntensity = (TextView)findViewById(R.id.uvIntensity);
- image = (ImageView)findViewById(R.id.image);
- }
3. 定义目标InputStream
- URL url = new URL(s);
- URLConnection connection = url.openConnection();
- HttpURLConnection httpConnection = (HttpURLConnection)connection;
- InputStream in = httpConnection.getInputStream();
- int responseCode = httpConnection.getResponseCode();
4. 定于DocumentBuilder实例 并解析目标
- DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
- DocumentBuilder db = dbfactory.newDocumentBuilder();
- //解析目标
- Document dom = db.parse(in);
5. 得到DocumentBuilder的所有Element
- Element docEle = dom.getDocumentElement();
6. 得到docEle 的"channel"分支
- NodeList nl = docEle.getElementsByTagName("channel");
7. 解析 并得到自己关系的列
- if (nl != null && nl.getLength() > 0) {
- for (int i = 0 ; i < nl.getLength(); i++) {
- //得到某行数据
- Element entry = (Element)nl.item(i);
- Element info = (Element)entry.getElementsByTagName("item").item(0);
- //从该行中取出目标 方法:键值 Key-Value
- Element eTitle = (Element)info.getElementsByTagName("title").item(0);
- Element eDay = (Element)info.getElementsByTagName("pubDate").item(0);
- Element eDescription = (Element)info.getElementsByTagName("description").item(0);
- //取出其内容
- String scity = eTitle.getFirstChild().getNodeValue();
- String stime = eDay.getFirstChild().getNodeValue();
- String sdescription = eDescription.getFirstChild().getNodeValue();
- //遍历目标 以指定字符分割 然后按顺序放入String[]
- String[] string = sdescription.split("<br>");
- String temporary = string[0];
- String tenp = temporary.split("=")[1];
- String humidity = string[1];
- String hum = humidity.split("=")[1];
- String uIndex = string[2];
- String uv_Index = uIndex.split("=")[1];
- String uIntensity = string[3];
- String uv_Intensity = uIntensity.split("=")[1];
- //String uIntensity = string[3];
- String icoName = string[5];
- String address = icoName.split(" ")[1];
- String address1 = address.split("=")[1];
- //去除两边的"\""
- String address2 = address1.replaceAll("\"", "");
- city.setText("地区:"+scity);
- time.setText("时间:"+stime);
- temp.setText("温度:"+tenp);
- hunidity.setText("湿度:"+hum);
- uvIndex.setText("紫外线指数:"+uv_Index);
- uvIntensity.setText("紫外线强度:"+uv_Intensity);
- image.setImageBitmap(queryImageByURI(ico_preface+address2));
- }
- }
8. 实话说 具体怎么一步步解析的很难说 大家自己看代码 应该能理解 具体做法:类似于:剥花生 即:一层层剥 直至露出里面的东西
9. 对了 还有 即:根据图片名字 下载之 然后显示之
- public final static String ico_preface = "http://202.140.96.134:8080/FS-RSS/";
- public Bitmap queryImageByURI(String iu){
- try{
- URL imgURL = new URL(iu);
- URLConnection conn = imgURL.openConnection();
- conn.connect();
- InputStream is = conn.getInputStream();
- BufferedInputStream bis = new BufferedInputStream(is);
- Bitmap bm = BitmapFactory.decodeStream(bis);
- bis.close();
- is.close();
- return bm;
- }catch(Exception e){
- return null;
- }
- }
10. emulator 运行截图 哦 对了 别忘了打开网络权限 即:
- <uses-permission android:name="android.permission.INTERNET"></uses-permission>