自己实现的sax XML解析,可能会有误
package com.agatha.bean;
public class OrderInfoBean {
private String order_id;
private String room_type;
private String table_no;
private String status;
public void setOrder_id(String order_id) {
this.order_id = order_id;
}
public String getOrder_id() {
return order_id;
}
public void setRoom_type(String room_type) {
this.room_type = room_type;
}
public String getRoom_type() {
return room_type;
}
public void setTable_no(String table_no) {
this.table_no = table_no;
}
public String getTable_no() {
return table_no;
}
public void setStatus(String status) {
this.status = status;
}
public String getStatus() {
return status;
}
}
//////////////////////////////////////////////////////////
package com.agatha.data_parse;
import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import com.agatha.bean.OrderInfoBean;
import com.agatha.net.NetUtil;
import com.agatha.net.Url;
import android.util.Log;
public class XMLParser extends DefaultHandler {
private List<OrderInfoBean> beans = null;
private OrderInfoBean bean;
private String tagName = null;
public List<OrderInfoBean> getOrderInfo() {
return beans;
}
@Override
public void startDocument() throws SAXException {
beans = new ArrayList<OrderInfoBean>();
}
@Override
public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
if (localName.equals("person")) {
bean = new OrderInfoBean();
bean.setOrder_id(atts.getValue("order_id"));
}
this.tagName = localName;
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
if (tagName != null) {
String data = new String(ch, start, length);
if (tagName.equals("room_type")) {
this.bean.setRoom_type(data);
} else if (tagName.equals("table_no")) {
this.bean.setTable_no(data);
} else if (tagName.equals("status")) {
this.bean.setStatus(data);
}
}
}
@Override
public void endElement(String uri, String localName, String name) throws SAXException {
if (localName.equals("orders")) {
beans.add(bean);
bean = null;
}
this.tagName = null;
}
//////
String apiKey = "apiKey=" + myapp.client_no + "&branchId=" + myapp.branch_no + "&orderType="
+ tableTypeId + "&orderStatus=2";
String url = Url.GETORDERINFO + apiKey;
String order_data = NetUtil.doGet(getActivity(), url);
if (!order_data.equals("")) {
try {
// 创建解析器
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser saxParser = spf.newSAXParser();
saxParser.setProperty("http://xml.org/sax/features/namespaces", true);
XMLParser handler = new XMLParser();
ByteArrayInputStream inStream = new ByteArrayInputStream(order_data.getBytes());
saxParser.parse(inStream, handler);
List<OrderInfoBean> lbe = handler.getOrderInfo();
Log.i(TAG, "============================================serverOrder_ id:"
+ lbe.get(0).getOrder_id());
inStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}