使用Pull解析Xml文件
用的的jar包:
http://pan.baidu.com/s/1bnFEKgB
http://pan.baidu.com/s/1mgoXWNm
***PullParseXml类***
package pulltest; import java.io.IOException; import java.io.Reader; import java.util.ArrayList; import java.util.List; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserFactory; /** * @author yanru.zhang * */ public class PullParseXml { public List<Book> list; public Book book; public List<Book> getList(Reader reader) { try { XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); XmlPullParser parser = factory.newPullParser(); parser.setInput(reader); int eventType = parser.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { switch (eventType) { case XmlPullParser.START_DOCUMENT: list = new ArrayList<Book>(); break; case XmlPullParser.START_TAG: if (parser.getName().equals("book")) { book = new Book(); }else if(parser.getName().equals("id")){ eventType = parser.next(); book.setId(Integer.parseInt(parser.getText())); }else if(parser.getName().equals("name")){ eventType = parser.next(); book.setName(parser.getText()); }else if(parser.getName().equals("price")){ eventType = parser.next(); book.setPrice(Float.parseFloat(parser.getText())); } break; case XmlPullParser.TEXT: break; case XmlPullParser.END_TAG: if(parser.getName().equals("book")){ list.add(book); } break; } eventType = parser.next(); } } catch (XmlPullParserException e) { // TODO 自动生成的 catch 块 System.out.println("***XmlPullParserException"); e.printStackTrace(); } catch (IOException e) { // TODO 自动生成的 catch 块 System.out.println("***IOException"); e.printStackTrace(); } return list; } }
***test类***
package pulltest; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.Reader; import java.util.List; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserFactory; public class test { public static void main(String[] args) { // TODO 自动生成的方法存根 try { Reader reader = new FileReader(new File("xml/book.xml")); PullParseXml parseXml = new PullParseXml(); List<Book> list = parseXml.getList(reader); for(Book book:list){ System.out.println(book.toString()); } } catch (FileNotFoundException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } }
***Book类***
package pulltest; public class Book { private int id; private String name; private float price; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public float getPrice() { return price; } public void setPrice(float price) { this.price = price; } @Override public String toString() { return "id:" + id + ", name:" + name + ", price:" + price; } }
***book.xml***
<?xml version="1.0" encoding="utf-8"?> <books> <book> <id>1</id> <name>Thinking In Java</name> <price>80.00</price> </book> <book> <id>2</id> <name>Core Java</name> <price>90.00</price> </book> <book> <id>3</id> <name>Hello, Andriod</name> <price>100.00</price> </book> </books>