转载文章请注明出处:http://blog.csdn.net/dangxw_/article/details/14643843
解析xml文档在很多时候都要用到,比如像音乐播放器的网络歌曲列表,应用软件的下载列表等等。想要更新时只需要更改服务器上的xml文档即可,然后客户端读取固定的xml来显示相应的列表,先给出xml文档
<?xml version="1.0" encoding="utf-8"?> <items> <item> <imageurl> http://alga7.com/pic/1.jpg </imageurl> <date>10.3</date> <imagename>a1</imagename> </item> <item> <imageurl> http://alga7.com/pic/2.jpg </imageurl> <date>10.3</date> <imagename>a2</imagename> </item> <item> <imageurl> http://alga7.com/pic/3.jpg </imageurl> <date>10.3</date> <imagename>a3</imagename> </item> <item> <imageurl> http://alga7.com/pic/4.jpg </imageurl> <date>10.3</date> <imagename>a4</imagename> </item> </items>
解析xml android上有三种方式:DOM,SAX,PULL。只讲述DOM方式,其他一些解析方式的讲解给出博客的链接:
http://www.jb51.net/article/36150.htm(PULL)
http://my.eoe.cn/guanmac/archive/15453.html http://android.yaohuiji.com/archives/935(SAX)
http://my.eoe.cn/bupt/archive/15129.html(三种方式综合分析)
DOM 方式的解析,是将整个xml文档的内容读取到内存中,是以树状结构存储,读取数据相对来讲比较方便,你可以像处理数组那样按照下标寻找某个子树。但是也正是这个原因,对于内容量较大的xml就不再适用,因为会耗费资源,而且检索速度的优点也不再存在。
public class XmlReadThread { private String location; Document document = null; DocumentBuilder builder = null; DocumentBuilderFactory factory; Element rootElement; NodeList list; HttpURLConnection connection; Context context; public XmlReadThread( String str) { location = str; InputStream in = openConn(); if(in!=null) { factory = DocumentBuilderFactory.newInstance(); try { builder = factory.newDocumentBuilder(); } catch (ParserConfigurationException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { document = builder.parse(in); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } rootElement = document.getDocumentElement(); if(location.equals(UrlAddress.ADDRESS_SEND))//因为该类的运用不只是对应以给出的xml所以解析时要加入判断 { list = rootElement.getElementsByTagName("Table"); } else list = rootElement.getElementsByTagName("item"); } } private InputStream openConn() { HttpURLConnection uc; InputStream is = null; try { URL url = new URL(location); uc = (HttpURLConnection) url.openConnection(); uc.connect(); is = uc.getInputStream(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return is; } public int getLength() { if(list!= null) { return list.getLength(); } else return 0; } public String getDate (int i) { if(list!=null) { Element element = (Element) list.item(0); return new String(element.getElementsByTagName("date").item(0).getFirstChild().getNodeValue()); } return null; } public String getContent (int i) { if(list!=null) { Element element = (Element) list.item(i); return new String(element.getElementsByTagName("content").item(0).getFirstChild().getNodeValue()); } else { return null; } } public String getAndroid (int i) { if(list!=null) { Element element = (Element) list.item(i); return new String(element.getElementsByTagName("android").item(0).getFirstChild().getNodeValue()); } else { return null; } } public String getImageUrl(int i) { if(list!=null) { Element element = (Element) list.item(i); return new String(element.getElementsByTagName("imageurl").item(0).getFirstChild().getNodeValue()); } return null; } public String getImageName(int i) { if(list!=null) { Element element = (Element) list.item(i); Log.d("!!!!!",String.valueOf(i)); return new String(element.getElementsByTagName("imagename").item(0).getFirstChild().getNodeValue()); } return null; } }
由于是通过网络读取远程服务器上的资源,所以在主线程中需要新启线程:
new Thread(){
File imagefile;
File[] fileList;
Bitmap bitmap;
String[] name = new String[16];
@Override
public void run()
{
handle.sendEmptyMessage(0x2223); //子线程与ui线程通信
XmlReadThread reader= new XmlReadThread(UrlAddress.ADDRESS_IMAGEFILE);
for(int i= 0 ; i < 16;i++)
{
name[i]=reader.getImageName(i) + reader.getDate(i);
fileList = file.listFiles();
imagefile = new File(file.getPath()+"/"+name[i]+".png");
Log.d("!!!!!!",imagefile.getPath());
if(!imagefile.exists())
{
if(fileList.length>i+1&&fileList[i]!= null)
{
fileList[i].delete();
}
try {
imagefile.createNewFile();
Log.d("!!!!",imagefile+"is creadte");
URL url = new URL(reader.getImageUrl(i));
InputStream is = url.openStream();
bitmap = BitmapFactory.decodeStream(is);
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(imagefile));
bitmap.compress(Bitmap.CompressFormat.PNG, 80, bos);//这行代码很重要,否则就会出现图片读取的乱码
bos.flush();
bos.close();
is.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
handle.sendEmptyMessage(0x2222);
}
}.start();
别忘了加入联网权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>//存取数据的权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
最后给出成品截图:沈阳南湖春色酒店的app(我也给投资人打个广告),这是广告墙的activity(其实是模仿 肯德基的宅急送,不过用它充当标签页的同时再包装成广告墙还是蛮有想法的,切换图片的特效显示不出来,可惜了。):