Dom解析xml

来源:互联网 发布:vb与三菱plc通讯 编辑:程序博客网 时间:2024/06/02 05:53

关于XML解析 以前有说过 不过那是SAX方式的 今天说一下DOM方法

 

 

[序言]

1. 今天解析的目标是:香港天气rss 地址为:

Java代码  收藏代码
  1. http://202.140.96.134:8080/FS-RSS/ftpfile/local_weather.xml  

 

现在的目标就是:定制化该目标的解析办法 我们还是查看一下该地址的源文件 具体方法:

 

 

现在贴其源文件://注:为了阅读方便 我加了一些“回车换行”源文件是没有这些的

Java代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <rss version="2.0">  
  3.    <channel><title>HK Weather Today</title>  
  4.               <description>HK Weather Today</description>  
  5.               <item><title>HK Weather Today</title>  
  6.                   <pubDate>Wed, 24 Mar 2010 07:00:00 GMT</pubDate>  
  7.                   <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>  
  8.          </item>  
  9.     </channel></rss>  

 

 

 

下面叙述具体怎么解析:

 

1. 定义界面 内有若干TextView 供显示结果用

 

Java代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView    
  8.     android:id="@+id/city"  
  9.     android:layout_width="fill_parent"   
  10.     android:layout_height="wrap_content"   
  11.     />  
  12. <TextView    
  13.     android:id="@+id/time"  
  14.     android:layout_width="fill_parent"   
  15.     android:layout_height="wrap_content"   
  16.     />  
  17. <TextView    
  18.     android:id="@+id/temp"  
  19.     android:layout_width="fill_parent"   
  20.     android:layout_height="wrap_content"   
  21.     />  
  22. <TextView    
  23.     android:id="@+id/humidity"  
  24.     android:layout_width="fill_parent"   
  25.     android:layout_height="wrap_content"   
  26.     />  
  27. <TextView    
  28.     android:id="@+id/uvIndex"  
  29.     android:layout_width="fill_parent"   
  30.     android:layout_height="wrap_content"   
  31.     />  
  32. <TextView    
  33.     android:id="@+id/uvIntensity"  
  34.     android:layout_width="fill_parent"   
  35.     android:layout_height="wrap_content"   
  36.     />  
  37. <ImageView    
  38.     android:id="@+id/image"  
  39.     android:layout_width="fill_parent"   
  40.     android:layout_height="wrap_content"   
  41.     />  
  42. </LinearLayout>  

 

 

2. 得到各个View 的实例

Java代码  收藏代码
  1. public void initialView(){  
  2.         city = (TextView)findViewById(R.id.city);  
  3.         time = (TextView)findViewById(R.id.time);  
  4.         temp = (TextView)findViewById(R.id.temp);  
  5.         hunidity = (TextView)findViewById(R.id.humidity);  
  6.         uvIndex = (TextView)findViewById(R.id.uvIndex);  
  7.         uvIntensity = (TextView)findViewById(R.id.uvIntensity);  
  8.         image = (ImageView)findViewById(R.id.image);  
  9.     }  

 

 

3. 定义目标InputStream

Java代码  收藏代码
  1. URL url = new URL(s);  
  2.   
  3. URLConnection connection = url.openConnection();  
  4.               
  5. HttpURLConnection httpConnection = (HttpURLConnection)connection;  
  6.   
  7. InputStream in = httpConnection.getInputStream();   
  8. int responseCode = httpConnection.getResponseCode();  
  9.               

 

 

4. 定于DocumentBuilder实例 并解析目标

Java代码  收藏代码
  1. DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();  
  2.   
  3. DocumentBuilder db = dbfactory.newDocumentBuilder();  
  4.   
  5. //解析目标  
  6. Document dom = db.parse(in);  

 

 

5. 得到DocumentBuilder的所有Element

Java代码  收藏代码
  1. Element docEle = dom.getDocumentElement();  

 

 

6. 得到docEle 的"channel"分支

Java代码  收藏代码
  1. NodeList nl = docEle.getElementsByTagName("channel");  

 

 

7. 解析 并得到自己关系的列

Java代码  收藏代码
  1. if (nl != null && nl.getLength() > 0) {  
  2.                     for (int i = 0 ; i < nl.getLength(); i++) {  
  3.                           
  4.                         //得到某行数据  
  5.                         Element entry = (Element)nl.item(i);  
  6.                         
  7.                         Element info = (Element)entry.getElementsByTagName("item").item(0);  
  8.                           
  9.                         //从该行中取出目标  方法:键值 Key-Value  
  10.                         Element eTitle = (Element)info.getElementsByTagName("title").item(0);  
  11.                         Element eDay = (Element)info.getElementsByTagName("pubDate").item(0);  
  12.                         Element eDescription = (Element)info.getElementsByTagName("description").item(0);  
  13.                           
  14.                         //取出其内容  
  15.                         String scity = eTitle.getFirstChild().getNodeValue();  
  16.                         String stime = eDay.getFirstChild().getNodeValue();  
  17.                         String sdescription = eDescription.getFirstChild().getNodeValue();  
  18.                           
  19.                         //遍历目标 以指定字符分割 然后按顺序放入String[]  
  20.                         String[] string = sdescription.split("<br>");  
  21.                           
  22.                         String temporary = string[0];  
  23.                         String tenp = temporary.split("=")[1];  
  24.                           
  25.                         String humidity = string[1];  
  26.                         String hum = humidity.split("=")[1];  
  27.                           
  28.                         String uIndex = string[2];  
  29.                         String uv_Index = uIndex.split("=")[1];  
  30.                           
  31.                         String uIntensity = string[3];  
  32.                         String uv_Intensity = uIntensity.split("=")[1];  
  33.                           
  34.                           
  35.                         //String uIntensity = string[3];  
  36.                         String icoName = string[5];  
  37.                           
  38.                         String address = icoName.split(" ")[1];  
  39.                           
  40.                         String address1 = address.split("=")[1];  
  41.                           
  42.                         //去除两边的"\""  
  43.                         String address2 = address1.replaceAll("\"""");  
  44.                           
  45.                           
  46.                         city.setText("地区:"+scity);  
  47.                         time.setText("时间:"+stime);  
  48.                         temp.setText("温度:"+tenp);  
  49.                         hunidity.setText("湿度:"+hum);  
  50.                         uvIndex.setText("紫外线指数:"+uv_Index);  
  51.                         uvIntensity.setText("紫外线强度:"+uv_Intensity);  
  52.                         image.setImageBitmap(queryImageByURI(ico_preface+address2));  
  53.                     }  
  54.                   }  

 

8. 实话说 具体怎么一步步解析的很难说 大家自己看代码 应该能理解 具体做法:类似于:剥花生 即:一层层剥 直至露出里面的东西

 

 

9. 对了 还有 即:根据图片名字 下载之 然后显示之

Java代码  收藏代码
  1. public final static String ico_preface = "http://202.140.96.134:8080/FS-RSS/";  
  2.   
  3. public Bitmap queryImageByURI(String iu){  
  4.         try{  
  5.               
  6.             URL imgURL = new URL(iu);  
  7.             URLConnection conn = imgURL.openConnection();  
  8.               
  9.             conn.connect();  
  10.             InputStream is = conn.getInputStream();  
  11.               
  12.             BufferedInputStream bis = new BufferedInputStream(is);  
  13.               
  14.             Bitmap bm = BitmapFactory.decodeStream(bis);  
  15.               
  16.             bis.close();  
  17.             is.close();  
  18.             return bm;  
  19.         }catch(Exception e){  
  20.           return null;  
  21.         }   
  22.     }  

  

10. emulator 运行截图 哦 对了 别忘了打开网络权限 即:

Java代码  收藏代码
  1. <uses-permission android:name="android.permission.INTERNET"></uses-permission>  

 

原创粉丝点击