XML
假如有这样一个XML格式的数据:
<?xml version="1.0" encoding="utf-8"?> <resources> <customer name="luopeng" age="21" gender="1" emial="dylankeepmoving@163.com"/> <customer name="dylan" age="22" gender="2" emial="710097663@qq.com"/> <customer name="android" age="6" gender="2" emial="android@gmail.com"/> </resources>
下面我们来编写一个类来读取,此类的作用是点击按钮后在EditView上显示资源文件(一个XML)中的内容,此例中会使用到XMLResourceParser
public class TestXmlResourceParserActivity extends Activity { private EditText et; private Button myButton; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // 变量在内部类中使用的前提是此变量必须是final修饰的 myButton = (Button) this.findViewById(R.id.btn01); et = (EditText) this.findViewById(R.id.edittext01); myButton.setOnClickListener(new OnClickListener() { StringBuilder sb = new StringBuilder(""); Resources res = getResources(); XmlResourceParser xrp = res.getXml(R.xml.test); @Override public void onClick(View v) { int counter = 0; try { // 判断是否到了文件的结尾 while (xrp.getEventType() != XmlResourceParser.END_DOCUMENT) { //文件的内容的起始标签开始,注意这里的起始标签是test.xml文件里面<resources>标签下面的第一个标签 if (xrp.getEventType() == XmlResourceParser.START_TAG) { String tagname = xrp.getName(); if (tagname.endsWith("customer")) { counter++; sb.append("这是第" + counter + "客户"+"\n"); sb.append("姓名:"+xrp.getAttributeValue(0)+"\n"); sb.append("年龄:"+xrp.getAttributeValue(1)+"\n"); sb.append("性别:"+xrp.getAttributeValue(2)+"\n"); sb.append("邮箱:"+xrp.getAttributeValue(3)+"\n\n"); } } xrp.next(); } et.setText(sb.toString()); } catch (XmlPullParserException e) { } catch (IOException e) { e.printStackTrace(); } } }); }
JSON
创建JSON数据
try { //首先创建对象中的一个单个键值对 JSONObject root = new JSONObject(); root.put("cat", "it"); //另一个键值对的值是一个数组,数组里装了3个JSON对象,所以再分别搞3个JSONObject对象存放键值对 JSONObject lan1 = new JSONObject(); lan1.put("id", "1"); lan1.put("ide", "Eclipse"); lan1.put("name","Java"); JSONObject lan2 = new JSONObject(); lan2.put("id", "2"); lan2.put("ide", "XCode"); lan2.put("name","Swift"); JSONObject lan3 = new JSONObject(); lan3.put("id", "2"); lan3.put("ide", "Visual Studio"); lan3.put("name","C#"); //在将这3个对象存入一个数组 JSONArray array = new JSONArray(); array.put(lan1); array.put(lan2); array.put(lan3); //再将数组作为键"languages"的值,构成一个对象 root.put("languages", array); //本例中只将内容输出出来 System.out.println(root.toString()); } catch (JSONException e) { e.printStackTrace(); }
读取JSON数据
assets/test.json
{ "languages":[ {"id":1,"ide":"Eclipse","name":"Java"}, {"id":2,"ide":"XCode","name":"Swift"}, {"id":3,"ide":"Visual Studio","name":"C#"}, ], "cat","it" } try { InputStream is = getResources().getAssets().open("test.json"); InputStreamReader isr = new InputStreamReader(is, "UTF-8"); BufferedReader br = new BufferedReader(isr); //将文本中的数据,都读取到一个StringBuilder当中 String line; StringBuilder builder = new StringBuilder(); while((line=br.readLine()) != null ){ builder.append(line); } br.close(); isr.close(); JSONObject root = new JSONObject(builder.toString()); //根据键,获得键对应的值,由于值是String类型,所以用getString System.out.println("cat="+root.getString("cat")); //根据键,获得键对应的数组,由于值是一个数组,所以用getJSONArray JSONArray array = root.getJSONArray("languages"); //由于要遍历数组,所以创建一个for循环 for(int i=0; i < array. length(); i++){ //由于每一个数组当中的元素,也是一个新的JSON对象 JSONObject lan = array.getJSONObject(i); System.out.println("-------------------"); System.out.println("id="+lan.getInt("id")); System.out.println("ide="+lan.getString("name")); System.out.println("name="+lan.getString("name")); Log.i("tag","-------"); } } catch (IOException e) { e.printStackTrace(); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); }