【Android开发】XML文件解析

最近在做一个项目,涉及到XML文件的解析,废话不多说,如下:



 

读取

 1 private ArrayList<Data> readXMLLocked() {
 2         File file = new File(mDirPath, mFileName);
 3         if (!file.exists()) {
 4             return null;
 5         }
 6         ArrayList list = new ArrayList<Data>();
 7         InputStream in = null;
 8         try {
 9             in = new FileInputStream(file);
10             XmlPullParser pullParser = Xml.newPullParser();
11             pullParser.setInput(in, "UTF-8");
12             int event = pullParser.getEventType();
13             while (event != XmlPullParser.END_DOCUMENT) {
14                 switch (event) {
15                     case XmlPullParser.START_DOCUMENT:
16                         break;
17                     case XmlPullParser.START_TAG:
18                         String name = pullParser.getName();
19                         ILog.d(TAG, "xmlname:" + name);
20                         if (TAG_ITEM.equals(name)) {
21                             int count = pullParser.getAttributeCount();
22                             Data data = new Data();
23                             for (int i = 0; i < count; i++) {
24                                 String attName = pullParser.getAttributeName(i);
25                                 if (ATT_DATA.equals(attName)) {
26                                     data.setmWeight(Float.valueOf(pullParser.getAttributeValue(i)));
27                                 } else if (ATT_TIME.equals(attName)) {
28                                     data.setTime(Long.valueOf(pullParser.getAttributeValue(i)));
29                                 }
30                             }
31                             list.add(data);
32                         } else if (TAG_VERSION.equals(name)) {
33 
34                         }
35                         break;
36                     case XmlPullParser.END_TAG:
37                         break;
38                 }
39                 event = pullParser.next();
40             }
41         } catch (Exception e) {
42             e.printStackTrace();
43             return null;
44         } finally {
45             try {
46                 in.close();
47             } catch (Exception e) {
48                 e.printStackTrace();
49             }
50         }
51         return list;
52     }

保存:

 1 private synchronized boolean saveXMLLocked(ArrayList<Data> list, OutputStream outStream) {
 2         try {
 3             XmlSerializer serializer = Xml.newSerializer();
 4             serializer.setOutput(outStream, "UTF-8");
 5             serializer.startDocument("UTF-8", true);
 6             serializer.startTag(null, TAG_IWEIHT);
 7             serializer.startTag(null, TAG_VERSION);
 8             serializer.text(String.valueOf(DBHelper.VERSION));
 9             serializer.endTag(null, TAG_VERSION);
10             for (Data data : list) {
11                 serializer.startTag(null, TAG_ITEM);
12                 serializer.attribute(null, ATT_DATA, String.valueOf(data.getWeight()));
13                 serializer.attribute(null, ATT_TIME, String.valueOf(data.getTime()));
14                 serializer.endTag(null, TAG_ITEM);
15             }
16             serializer.endTag(null, TAG_IWEIHT);
17             serializer.endDocument();
18         } catch (Exception e) {
19             e.printStackTrace();
20             return false;
21         } finally {
22             try {
23                 outStream.flush();
24                 outStream.close();
25             } catch (Exception e) {
26                 e.printStackTrace();
27             }
28         }
29         return true;
30     }

对应的XML文件的格式如下:

1 <?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
2 <iweight>
3     <version>1</version>
4         <item data="25.0" time="1482927231388" />
5         <item data="75.0" time="1482927234989" />
6         <item data="48.0" time="1482927238008" />
7 </iweight>

 

posted @ 2016-12-28 20:16  程小白  阅读(314)  评论(0编辑  收藏  举报