标签:
1、xml文件,xml文件在res/xml目录下
1 <?xml version="1.0" encoding="utf-8"?> 2 <infos> 3 <city id="1"> 4 <temp>20C/30C</temp> 5 <weather>多云转晴</weather> 6 <wind>7-8级</wind> 7 <name>广州</name> 8 <pm>200</pm> 9 </city> 10 <city id="2"> 11 <temp>25C/30C</temp> 12 <weather>多云转晴</weather> 13 <wind>2-3级</wind> 14 <name>钦州</name> 15 <pm>100</pm> 16 </city> 17 <city id="3"> 18 <temp>20C/30C</temp> 19 <weather>多云转晴</weather> 20 <wind>7-9级</wind> 21 <name>北海</name> 22 <pm>250</pm> 23 </city> 24 </infos>
1 <?xml version="1.0" encoding="UTF-8"?> 2 <plugin> 3 <server> 4 <className>Demo</className> 5 <serverName>显示信息</serverName> 6 <methodName>showDemo</methodName> 7 </server> 8 <server> 9 <className>Demo</className> 10 <serverName>bbs</serverName> 11 <methodName>doBbs</methodName> 12 </server> 13 </plugin>
2、xml文件
1 package com.zyh.androidpathtest; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import android.os.Bundle; 7 import android.app.Activity; 8 import android.content.res.XmlResourceParser; 9 import android.text.TextUtils; 10 import android.view.Menu; 11 import android.view.View; 12 13 public class MainActivity extends Activity { 14 15 @Override 16 protected void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 setContentView(R.layout.activity_main); 19 } 20 21 public void click(View view){ 22 try { 23 XmlResourceParser parser = getResources().getXml(R.xml.weather); 24 int type = parser.getEventType(); 25 while(type != XmlResourceParser.END_DOCUMENT){ 26 switch(type){ 27 case XmlResourceParser.START_TAG: 28 if("infos".equals(parser.getName())){ 29 }else if("city".equals(parser.getName())){ 30 String idStr = parser.getAttributeValue(0); 31 System.out.println("idStr=" + idStr); 32 }else if("temp".equals(parser.getName())){ 33 String temp = parser.nextText(); 34 System.out.println("temp=" + temp); 35 }else if("weather".equals(parser.getName())){ 36 String weather = parser.nextText(); 37 System.out.println("weather=" + weather); 38 }else if("wind".equals(parser.getName())){ 39 String wind = parser.nextText(); 40 System.out.println("wind=" + wind); 41 }else if("name".equals(parser.getName())){ 42 String name = parser.nextText(); 43 System.out.println("name=" + name); 44 }else if("pm".equals(parser.getName())){ 45 String pm = parser.nextText(); 46 System.out.println("pm=" + pm); 47 } 48 break; 49 case XmlResourceParser.END_TAG: 50 if("city".equals(parser.getName())){ 51 System.out.println("==================================="); 52 } 53 default: 54 break; 55 } 56 57 58 type = parser.next(); 59 } 60 } catch (Exception e) { 61 // TODO Auto-generated catch block 62 e.printStackTrace(); 63 } 64 } 65 66 public void click2(View view){ 67 try{ 68 XmlResourceParser parser = getResources().getXml(R.xml.plugin_server); 69 int type = parser.getEventType(); 70 PluginServer ps=null; 71 List<PluginServer> pss = null; 72 while(type != XmlResourceParser.END_DOCUMENT){ 73 switch(type){ 74 case XmlResourceParser.START_TAG: 75 if(TextUtils.equals("plugin", parser.getName())){ 76 pss = new ArrayList<PluginServer>(); 77 }else if(TextUtils.equals("server", parser.getName())){ 78 ps = new PluginServer(); 79 }else if(TextUtils.equals("className", parser.getName())){ 80 String className = parser.nextText(); 81 ps.setClassName(className); 82 }else if(TextUtils.equals("serverName", parser.getName())){ 83 String serverName = parser.nextText(); 84 ps.setServerName(serverName); 85 }else if(TextUtils.equals("methodName", parser.getName())){ 86 String methodName = parser.nextText(); 87 ps.setMethodName(methodName); 88 } 89 break; 90 case XmlResourceParser.END_TAG: 91 if(TextUtils.equals("server", parser.getName())){ 92 pss.add(ps); 93 } 94 break; 95 default: 96 break; 97 } 98 type = parser.next(); 99 } 100 101 for(PluginServer pgs : pss){ 102 System.out.println(pgs.toString()); 103 } 104 }catch(Exception e){ 105 e.printStackTrace(); 106 } 107 } 108 109 }
标签:
原文地址:http://www.cnblogs.com/zhongyinghe/p/5449673.html