zoukankan      html  css  js  c++  java
  • XML文件的解析和序列化

    序列化:

    private void createXml() {  
            XmlSerializer serializer = Xml.newSerializer();// xml文件生成器  
            File file = new File(Environment.getExternalStorageDirectory(),  
                    "person.xml");  
            FileOutputStream fos = null;  
            try {  
                fos = new FileOutputStream(file);  
                serializer.setOutput(fos, "utf-8");// 为xml生成器设置输出流和字符编码
           
           serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output",true);//设置xml文件的自动格式化
           serializer.startDocument("utf-8", true);// 开始文档,参数分别为字符编码和是否保持独立 
                serializer.startTag(null, "persons"); // 开始标签,参数分别为:命名空间和标签名
                for (Person person : list) {  
      
                    serializer.startTag(null, "person");  //对象的开始标签(对象为list集合的每一个元素)
                    serializer.attribute(null, "id", person.getId() + "");  //对象开始标签的属性
      
                    serializer.startTag(null, "name");// 开始标签  
                    serializer.text(person.getName());// 文本内容  
                    serializer.endTag(null, "name");// 结束标签  
      
                    serializer.startTag(null, "sex");  
                    serializer.text(person.getSex());  
                    serializer.endTag(null, "sex");  
      
                    serializer.startTag(null, "age");  
                    serializer.text(person.getAge());  
                    serializer.endTag(null, "age");  
      
                    serializer.startTag(null, "address");  
                    serializer.text(person.getAddress());  
                    serializer.endTag(null, "address");  
      
                    serializer.endTag(null, "person");  //对象的结束标签
      
                }  
                serializer.endTag(null, "persons");// 结束标签  
                serializer.endDocument();// 结束xml文档  
                Toast.makeText(getApplicationContext(), "生成成功!", Toast.LENGTH_SHORT);  
            } catch (Exception e) {  
                Toast.makeText(getApplicationContext(), "生成失败!", Toast.LENGTH_SHORT);  
                e.printStackTrace();  
            } finally {  
                try {  
                    fos.close();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
            }  
      
        }  

    解析:

        private List<Person> pullXml() {
            try {
                File file = new File(Environment.getExternalStorageDirectory(),
                        "person.xml");
                FileInputStream fis = new FileInputStream(file);
                List<Person> persons = null;
                Person person = null;
                XmlPullParser parser = Xml.newPullParser();// 获取xml解析器
                parser.setInput(fis, "utf-8");// 参数分别为输入流和字符编码
                int type = parser.getEventType();
                while (type != XmlPullParser.END_DOCUMENT) {// 如果事件不等于文档结束事件就继续循环
                    switch (type) {
                    case XmlPullParser.START_TAG:    //开始标签
                        if ("persons".equals(parser.getName())) {//代表对象所在集合的开始标签
                            persons = new ArrayList<Person>();
                        } else if ("person".equals(parser.getName())) {//代表一个对象的开始;标签
                            person = new Person();
                            String id = parser.getAttributeValue(0);//得到标签的属性
                            person.setId(Integer.parseInt(id));
                        } else if ("name".equals(parser.getName())) {//对象的name属性的开始标签
                            person.setName(parser.nextText());  //得到开始标签和结束标签之间的文本内容
                        } else if ("sex".equals(parser.getName())) {//对象的sex属性的开始标签
                            person.setSex(parser.nextText());
                        } else if ("address".equals(parser.getName())) {//对象的address属性的开始标签
                            person.setAddress(parser.nextText());
                        } else if ("age".equals(parser.getName())) {  //对象的age属性的开始标签
                            person.setAge(parser.nextText());
                        }
                        break;
                    case XmlPullParser.END_TAG:  //结束标签
                        if ("person".equals(parser.getName())) {  //当得到代表对象的结束标签时,说明一个对象的数据采集完毕,将对象添加进集合
                            persons.add(person);
                            person = null;
                        }
                        break;
                    }
                    type = parser.next();// 继续往下查找
                }
                return persons;
            } catch (NumberFormatException e) {
                e.printStackTrace();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (XmlPullParserException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

     

  • 相关阅读:
    CodeForces
    CodeForces
    CodeForces 718C && HDU 3572 && Constellation
    CodeForces
    USACO 5.4 tour的dp解法
    10.22~10.28一周经典题目整理(meeting,BZOJ4377,POJ3659)
    codeforces 724D
    codeforces 724C
    hdu5909 Tree Cutting
    hdu5822 color
  • 原文地址:https://www.cnblogs.com/fengchuxiaodai/p/5167354.html
Copyright © 2011-2022 走看看