SAX解析多层嵌套XML
<?xml version="1.0" encoding="UTF-8"?> <Root> <Result>1</Result> <Message>查询成功</Message> <BookCode>20160720_D018</BookCode> <OrderDate>2016/7/20 17:11:09</OrderDate> <ConsumeID>8039</ConsumeID> <Bill> <BillList> <Place>出发台</Place> <SumMoney>1340.00</SumMoney> <PlayDate>2016-07-20</PlayDate> <BillItem> <ConsumeID>8039</ConsumeID> <GuestName>admin</GuestName> <StatusName>嘉宾</StatusName> <ServerName>打球费</ServerName> <ServerPrice>880.00</ServerPrice> <Amount>1.00</Amount> <SumDue>880.00</SumDue> </BillItem> <BillItem> <ConsumeID>8039</ConsumeID> <GuestName>admin</GuestName> <StatusName>嘉宾</StatusName> <ServerName>货物费</ServerName> <ServerPrice>200.00</ServerPrice> <Amount>1.00</Amount> <SumDue>200.00</SumDue> </BillItem> <BillItem> <ConsumeID>8039</ConsumeID> <GuestName>admin</GuestName> <StatusName>嘉宾</StatusName> <ServerName>单车差</ServerName> <ServerPrice>130.00</ServerPrice> <Amount>1.00</Amount> <SumDue>130.00</SumDue> </BillItem> </BillList> <BillList> <Place>名扬餐厅</Place> <SumMoney>1340.00</SumMoney> <PlayDate>2016-07-20</PlayDate> <BillItem> <ConsumeID>8039</ConsumeID> <GuestName>admin</GuestName> <StatusName>嘉宾</StatusName> <ServerName>货物费</ServerName> <ServerPrice>880.00</ServerPrice> <Amount>1.00</Amount> <SumDue>880.00</SumDue> </BillItem> <BillItem> <ConsumeID>8039</ConsumeID> <GuestName>admin</GuestName> <StatusName>嘉宾</StatusName> <ServerName>货物费</ServerName> <ServerPrice>200.00</ServerPrice> <Amount>1.00</Amount> <SumDue>200.00</SumDue> </BillItem> <BillItem> <ConsumeID>8039</ConsumeID> <GuestName>admin</GuestName> <StatusName>嘉宾</StatusName> <ServerName>单车差</ServerName> <ServerPrice>130.00</ServerPrice> <Amount>1.00</Amount> <SumDue>130.00</SumDue> </BillItem> </BillList> </Bill> </Root>
// 解析文档 ArrayList<BillList> parseXMLFile() throws Exception { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); InputStream is = this.getAssets().open("beauties.xml"); StudentSax handle = new StudentSax(); saxParser.parse(is, handle); is.close(); return handle.getLessons(); }
BillList model,保存最外层实体类
package com.example.saxdemo; import java.util.ArrayList; import java.util.Set; /** * * * @author dl * */ public class BillList { private String Place; private String SumMoney; private String PlayDate; private ArrayList<BillItem> billItems; public String getPlace() { return Place; } public void setPlace(String place) { Place = place; } public String getSumMoney() { return SumMoney; } public void setSumMoney(String sumMoney) { SumMoney = sumMoney; } public String getPlayDate() { return PlayDate; } public void setPlayDate(String playDate) { PlayDate = playDate; } public ArrayList<BillItem> getBillItems() { return billItems; } public void setBillItems(ArrayList<BillItem> billItems) { this.billItems = billItems; } public BillList(String place, String sumMoney, String playDate, ArrayList<BillItem> billItems) { super(); Place = place; SumMoney = sumMoney; PlayDate = playDate; this.billItems = billItems; } public BillList() { } }
BillItem model实体类,保存BillItem节点下数据
package com.example.saxdemo; import java.util.Set; /** * * * @author dl * */ public class BillItem { private String ConsumeID; private String GuestName; private String StatusName; private String ServerName; private String ServerPrice; private String Amount; private String SumDue; public String getConsumeID() { return ConsumeID; } public void setConsumeID(String consumeID) { ConsumeID = consumeID; } public String getGuestName() { return GuestName; } public void setGuestName(String guestName) { GuestName = guestName; } public String getStatusName() { return StatusName; } public void setStatusName(String statusName) { StatusName = statusName; } public String getServerName() { return ServerName; } public void setServerName(String serverName) { ServerName = serverName; } public String getServerPrice() { return ServerPrice; } public void setServerPrice(String serverPrice) { ServerPrice = serverPrice; } public String getAmount() { return Amount; } public void setAmount(String amount) { Amount = amount; } public String getSumDue() { return SumDue; } public void setSumDue(String sumDue) { SumDue = sumDue; } public BillItem(String consumeID, String guestName, String statusName, String serverName, String serverPrice, String amount, String sumDue) { super(); ConsumeID = consumeID; GuestName = guestName; StatusName = statusName; ServerName = serverName; ServerPrice = serverPrice; Amount = amount; SumDue = sumDue; } public BillItem() { } }
StudentSax 解析类
package com.example.saxdemo; import java.util.ArrayList; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class StudentSax extends DefaultHandler { private BillList lesson; private ArrayList<BillList> lessons; private ArrayList<BillItem> billItems; private String preTag; private BillItem billItem; @Override public void startDocument() throws SAXException { lessons = new ArrayList<BillList>(); billItems= new ArrayList<BillItem>(); } @Override public void characters(char[] ch, int start, int length) throws SAXException { if (lesson != null) { String data = new String(ch, start, length); if ("Place".equals(preTag)) { lesson.setPlace(data); } if ("SumMoney".equals(preTag)) { lesson.setSumMoney(data); } if ("PlayDate".equals(preTag)) { lesson.setPlayDate(data); } if ("ConsumeID".equals(preTag)) { billItem.setConsumeID(data); } if ("GuestName".equals(preTag)) { billItem.setGuestName(data); } if ("StatusName".equals(preTag)) { billItem.setStatusName(data); } if ("ServerName".equals(preTag)) { billItem.setServerName(data); } if ("ServerPrice".equals(preTag)) { billItem.setServerPrice(data); } if ("Amount".equals(preTag)) { billItem.setAmount(data); } if ("SumDue".equals(preTag)) { billItem.setSumDue(data); } } } @Override public void startElement(String uri, String localName, String name, Attributes attr) throws SAXException { if ("BillList".equals(name)) { //遇到BillList节点,new一个对象 lesson = new BillList(); } if ("BillItem".equals(name)) { //遇到BillItem节点,new一个对象 billItem = new BillItem(); } preTag = name; } @Override public void endElement(String uri, String localName, String name) throws SAXException { if (lesson != null && "BillList".equals(name)) { //子节点下面的billItems加入lesson列表中 lesson.setBillItems(billItems); lessons.add(lesson); lesson = null; billItems= new ArrayList<BillItem>(); } if (billItem != null && "BillItem".equals(name)) { //子节点下面的billItem单条数据加入billItems列表中 billItems.add(billItem); billItem = null; } preTag = null; } public BillList getLesson() { return lesson; } public void setLesson(BillList lesson) { this.lesson = lesson; } public ArrayList<BillList> getLessons() { return lessons; } public void setLessons(ArrayList<BillList> lessons) { this.lessons = lessons; } public ArrayList<BillItem> getBillItems() { return billItems; } public void setBillItems(ArrayList<BillItem> billItems) { this.billItems = billItems; } public String getPreTag() { return preTag; } public void setPreTag(String preTag) { this.preTag = preTag; } public BillItem getBillItem() { return billItem; } public void setBillItem(BillItem billItem) { this.billItem = billItem; } }