Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發實例 >> 使用Pull方法生成一個XML 並解析

使用Pull方法生成一個XML 並解析

編輯:Android開發實例

  1.首先在SD卡上新建一個測試文件夾,用來保存生成的XML

  // 獲取保存路徑
      public File getFilePath() {
         File filePath = null;
         //判斷SD卡存在與否
         if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
             filePath = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+

  "/A_Test/test/");
             if (!filePath.isDirectory()) {//判斷文件存在與否,不存在就創建
                filePath.mkdirs();
             }
         } else {
             Toast.makeText(Main.this, "存儲卡不存在,請插入卡!", 3000).show();
         }
         return filePath;
      }

  注意:權限的添加:
  <!-- SD卡寫入 -->
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  <uses-permission android:name="android.permission.DELETE_CACHE_FILES" />

  2.生成一個測試的XML文件

  File file = new File(getFilePath() + "/test.xml");
  if (!file.exists()) {// 如果文件不存在,就創建新文件
                System.out.println("文件不存在,馬上創建!");
                List<PersonModel> personModel = new ArrayList<PersonModel>();
                personModel.add(new PersonModel(1001, "張三", (short) 30));
                personModel.add(new PersonModel(1002, "李四", (short) 18));
                personModel.add(new PersonModel(1003, "王二", (short) 21));
                CreateXML.createXML(file, personModel);
  }
  /**
   * 生成XML文件
   *
   * @author lilin
   * @date 2011-9-26 上午08:36:08
   * @ClassName: CreateXML
   * @Description: TODO
   */
  public class CreateXML {
      // 生成XML文件
      public static void createXML(File file, List<PersonModel> personModel)
             throws IllegalArgumentException, IllegalStateException, IOException {
         FileOutputStream outputStream = null;
         OutputStreamWriter writer = null;
         outputStream = new FileOutputStream(file);
         writer = new OutputStreamWriter(outputStream, "UTF-8");
         BufferedWriter writer2 = new BufferedWriter(writer);// 增加的緩存功能
         XmlSerializer serializer = Xml.newSerializer();
         serializer.setOutput(writer2);
         serializer.setOutput(outputStream, "utf-8");
         serializer.startDocument("utf-8", true);
         serializer.startTag(null, "persons");
         for (PersonModel persons : personModel) {
             serializer.startTag(null, "person");
             serializer.attribute(null, "id", persons.getId().toString());
           
             serializer.startTag(null, "name");
             serializer.text(persons.getName());
             serializer.endTag(null, "name");
           
             serializer.startTag(null, "age");
             serializer.text(persons.getAge().toString());
             serializer.endTag(null, "age");
          
             serializer.endTag(null, "person");
         }
         serializer.endTag(null, "persons");
         serializer.endDocument();
         outputStream.close();
      }
  }

  3.解析XML
  FileInputStream fileInputStream = new FileInputStream(file);
             List<PersonModel> personModel = PullXML.pullXML(fileInputStream);
             for (int i = 0; i < personModel.size(); i++) {
                System.out.println(personModel.get(i).getId() + "--"
                       + personModel.get(i).getName() + "--"
                       + personModel.get(i).getAge());
             }
  
  /**
   * 解析XML
   *
   * @author lilin
   * @date 2011-9-26 上午09:23:37
   * @ClassName: PullXML
   * @Description: TODO
   */
  public class PullXML {
      public static List<PersonModel> pullXML(InputStream inStream)
             throws Throwable {
         List<PersonModel> persons = null;
         PersonModel person = null;
         /* 聲明XML的pull解析器 */
         XmlPullParser parser = Xml.newPullParser();
         /* 聲明編碼方式 */
         parser.setInput(inStream, "UTF-8");
         int eventType = parser.getEventType();// 產生第一個事件
         while (eventType != XmlPullParser.END_DOCUMENT) {// 只要不是文檔結束事件
             switch (eventType) {
             case XmlPullParser.START_DOCUMENT:// 0 文檔開始事件
                persons = new ArrayList<PersonModel>();
                break;
  
             case XmlPullParser.START_TAG:// 2 開始元素
                String name = parser.getName();// 獲取解析器當前指向的元素的名稱
                if ("person".equals(name)) {
                    person = new PersonModel();
                    person.setId(new Integer(parser.getAttributeValue(0)));
                }
                if (person != null) {
                    if ("name".equals(name)) {
                       person.setName(parser.nextText());// 獲取解析器當前指向元素的下一個文本節點的值
                    }
                    if ("age".equals(name)) {
                       person.setAge(new Short(parser.nextText()));
                    }
                }
                break;
   
             case XmlPullParser.END_TAG:
                if ("person".equals(parser.getName())) {
                    persons.add(person);
                    person = null;
                }
                break;
             }
             eventType = parser.next();
         }
         return persons;
      }
  }

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved