复制代码 代码如下:
public class SaxParse{
private SAXParser parser;
public SaxParse(){
try {
SAXParserFactory f = SAXParserFactory.newInstance();
parser = f.newSAXParser();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public List
try {
XmlHandler h = new XmlHandler();
parser.parse(is,h);
return h.getpersons();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
class XmlHandler extends DefaultHandler{
List
Person person = null ;
//当前元素名称
private String currEleName;
public void characters(char[] ch, int start, int length)throws SAXException {
String str = new String(ch,start,length);
//name
if("name".equals(currEleName)){
person.name = str ;
}
else if("age".equals(currEleName)){
person.age = Integer.parseInt(str);
}
}
public void enddocument() throws SAXException {
}
public void endElement(String uri, String localName, String qName)
throws SAXException {
if("person".equals(localName)){
persons.add(person);
}
//将当前元素置空
else if(("name".equals(currEleName)) || ("age".equals(currEleName))){
this.currEleName = "" ;
}
}
public void startdocument() throws SAXException {
persons = new ArrayList
}
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
//实例化person对象
if("person".equals(localName)){
person = new Person();
person.id = Integer.parseInt(attributes.getValue(0));
}
//name元素
else if("name".equals(localName)){
this.currEleName = "name" ;
}
//name元素
else if("age".equals(localName)){
this.currEleName = "age" ;
}
}
public List
return persons ;
}
}
}
2.dom方式
复制代码 代码如下:
public class DomParse{
//
private documentBuilder builder;
public DomParse(){
try {
documentBuilderFactory f = documentBuilderFactory.newInstance();
this.builder = f.newdocumentBuilder();
} catch (Exception e) {
e.printStackTrace();
}
}
public List
List
Person person = null ;
try {
document doc = builder.parse(is);
NodeList list = doc.getElementsByTagName("person");
Element ele = null ;
for(int i = 0 ; i < list.getLength() ; i ++){
ele = (Element) list.item(i);
person = new Person();
person.id = Integer.parseInt(ele.getAttribute("id"));
person.name = getSubElementTextContent(ele,"name");
person.age = Integer.parseInt(getSubElementTextContent(ele,"age"));
persons.add(person);
}
} catch (Exception e) {
e.printStackTrace();
}
return persons;
}
private String getSubElementTextContent(Element ele, String tagName) {
NodeList list = ele.getElementsByTagName(tagName);
Element e = (Element) list.item(0);
//得到中间的文本节点
return e.getTextContent();
}
}
3.pull方式
复制代码 代码如下:
public class PullParse{
public List
List
Person person = null ;
try {
XmlPullParser parser = Xml.newPullParser();
//设置解析数据源
parser.setInput(is, "utf-8");
//取得事件的类型
int eventType = parser.getEventType();
String eleName = null ;
while(eventType != XmlPullParser.END_document){
switch(eventType){
//文档开始
case XmlPullParser.START_document:
persons = new ArrayList
break ;
//元素开始
case XmlPullParser.START_TAG:
eleName = parser.getName();
if("person".equals(eleName)){
person = new Person();
person.id = Integer.parseInt(parser.getAttributevalue(0));
}
else if("name".equals(eleName)){
person.name = parser.nextText();
}
else if("age".equals(eleName)){
person.age = Integer.parseInt(parser.nextText());
}
break ;
//标记结束
case XmlPullParser.END_TAG:
eleName = parser.getName();
if("person".equals(eleName)){
persons.add(person);
}
break ;
}
//手动激活下个事件的触发
eventType = parser.next();
}
} catch (Exception e) {
e.printStackTrace();
}
return persons;
}
}



