读书人

采取PULL解析XML

发布时间: 2012-06-29 15:48:47 作者: rapoo

采用PULL解析XML

/* * 读取 */public void testReadPull() throws ParserConfigurationException,SAXException, IOException, XmlPullParserException{PullXml sax = new PullXml();InputStream inStream = this.getClass().getClassLoader().getResourceAsStream("secn.xml");List<Person> persons = sax.domParser(inStream);for (Person person : persons){Log.i("SaxTest", String.valueOf(person.getName()));}}/* * 保存 */public void testReadPullSave() throws ParserConfigurationException,SAXException, IOException, XmlPullParserException{FileOutputStream fos = this.getContext().openFileOutput("pull.xml", Context.MODE_APPEND);List<Person> persons = new ArrayList<Person>();persons.add(new Person(1,"xiaoli",(short)12));persons.add(new Person(1,"xiaoli",(short)12));persons.add(new Person(1,"xiaoli",(short)12));persons.add(new Person(1,"xiaoli",(short)12));persons.add(new Person(1,"xiaoli",(short)12));PullXml sax = new PullXml();sax.save(persons, fos);}
?/** * * @param persons 集合 * @param outStream 要输出到的地方 * @throws IllegalArgumentException * @throws IllegalStateException * @throws IOException */public void save(List<Person> persons,OutputStream outStream) throws IllegalArgumentException, IllegalStateException, IOException{XmlSerializer serializer = Xml.newSerializer();serializer.setOutput(outStream, "utf-8");serializer.startDocument("utf-8", true);serializer.startTag(null, "persons");for(Person person:persons){serializer.startTag(null, "person");serializer.attribute(null, "id", String.valueOf(person.getId()));serializer.startTag(null, "name");serializer.text(person.getName());serializer.endTag(null, "name");serializer.startTag(null, "age");serializer.text(String.valueOf(person.getAge()));serializer.endTag(null, "age");serializer.endTag(null, "person");}serializer.endTag(null, "persons");serializer.endDocument();outStream.flush();outStream.close();}/** * 解析后并返回一个对象集合 * * @param inStream * xml输入流 * @return 解析后并返回一个对象集合 * @throws ParserConfigurationException * @throws SAXException * @throws IOException * @throws XmlPullParserException */@SuppressWarnings("static-access")public List<Person> domParser(InputStream inStream) throws ParserConfigurationException, SAXException, IOException, XmlPullParserException{List<Person> persons = null;Person person = null;XmlPullParserFactory pullFactory = XmlPullParserFactory.newInstance();XmlPullParser pullPerser = pullFactory.newPullParser();pullPerser.setInput(inStream, "utf-8");int eventType = pullPerser.getEventType();while(eventType != XmlPullParser.END_DOCUMENT){switch (eventType) {case XmlPullParser.START_DOCUMENT:persons = new ArrayList<Person>();break;case XmlPullParser.START_TAG:if("person".equals(pullPerser.getName())){person = new Person();person.setId(new Integer(pullPerser.getAttributeValue(0)));}if("name".equals(pullPerser.getName())){person.setName(pullPerser.nextText());}else if("age".equals(pullPerser.getName())){person.setAge(new Short(pullPerser.nextText()));}break;case XmlPullParser.END_TAG:if("person".equals(pullPerser.getName())){persons.add(person);}break;}eventType = pullPerser.next();}return persons;}

?

读书人网 >XML SOAP

热点推荐