SAX解析: |
// 得到工厂类 |
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); |
// 得到解析类 |
try { |
SAXParser saxParser = saxParserFactory.newSAXParser(); |
// 得到读取类 |
XMLReader xmlReader = saxParser.getXMLReader(); |
// 设置内容处理者 |
xmlReader.setContentHandler( new DefaultHandler() { |
// 读取文本 |
// 读取到结束标签 |
@Override |
public void endElement(String uri, String localName, |
String qName) throws SAXException { |
if (localName.equals( "student" )) { |
list.add(student); |
} |
tagName = "" ; |
super .endElement(uri, localName, qName); |
} |
@Override |
public void characters( char [] ch, int start, int length) |
throws SAXException { |
String tag = new String(ch); |
if ( "name" .equals(tagName)) { |
Log.i( "ASD" , "开始解析文件" +tag); |
student.setName(tag.trim()); |
} else if ( "sex" .equals(tagName)) { |
student.setSex(tag.trim()); |
} else if ( "address" .equals(tagName)) { |
student.setAddress(tag.trim()); |
} else if ( "hobby" .equals(tagName)) { |
student.setHobby(tag.trim()); |
} |
|
super .characters(ch, start, length); |
} |
// 文档读取完毕 |
@Override |
public void endDocument() throws SAXException { |
Log.i( "ASD" , "文件解析完毕" ); |
super .endDocument(); |
} |
// 开始读取文档 |
@Override |
public void startDocument() throws SAXException { |
Log.i( "ASD" , "开始解析文件" ); |
super .startDocument(); |
} |
// 读取到开始标签 |
@Override |
public void startElement(String uri, String localName, |
String qName, Attributes attributes) |
throws SAXException { |
tagName = localName; |
if (tagName.equals( "student" )) { |
Log.i( "ASD" , "开始解析文件" +tagName); |
student = new Student(); |
String id = attributes.getValue( "id" ); |
String age = attributes.getValue( "age" ); |
student.setAge(age.trim()); |
} |
super .startElement(uri, localName, qName, attributes); |
} |
}); |
xmlReader.parse( new InputSource(getAssets().open( "student.xml" ))); |
} catch (ParserConfigurationException e) { |
// TODO Auto-generated catch block |
e.printStackTrace(); |
} catch (SAXException e) { |
// TODO Auto-generated catch block |
e.printStackTrace(); |
} catch (IOException e) { |
// TODO Auto-generated catch block |
e.printStackTrace(); |
} |
PULL解析: |
// 获取XML文件 |
InputStream inputStream = getAssets().open( "student.xml" ); |
pullParser = Xml.newPullParser(); |
// 给解析对象设置输入流 |
pullParser.setInput(inputStream, "utf_8" ); |
// 获取xml的标签类型 |
int type = pullParser.getEventType(); |
// 当没有获取到结束标签时循环执行 |
while (type != XmlPullParser.END_DOCUMENT) { |
switch (type) { |
// 开始标签 |
case XmlPullParser.START_TAG: |
// 获取开始标签名称 |
name = pullParser.getName(); |
if (name.equals( "student" )) { |
student = new Student(); |
// 获取开始标签的属性 |
//String id = pullParser.getAttributeValue(0); |
String age = pullParser.getAttributeValue( 1 ); |
student.setAge(age); |
} else if (name.equals( "name" )) { |
String name = pullParser.nextText(); |
student.setName(name); |
} else if (name.equals( "sex" )) { |
String sex = pullParser.nextText(); |
student.setSex(sex); |
} else if (name.equals( "address" )) { |
String address = pullParser.nextText(); |
student.setAddress(address); |
} else if (name.equals( "hobby" )) { |
String hobby = pullParser.nextText(); |
student.setHobby(hobby); |
} |
break ; |
// 结束标签 |
case XmlPullParser.END_TAG: |
name = pullParser.getName(); |
if ( "student" .equals(name)) { |
// 将当前对象添加到集合 |
list.add(student); |
student = null ; |
} |
break ; |
} |
type = pullParser.next(); |
} |
|
} catch (IOException e) { |
// TODO Auto-generated catch block |
|
System.out.println(e.getMessage()); |
e.printStackTrace(); |
} catch (XmlPullParserException e) { |
System.out.println(e.getMessage()); |
e.printStackTrace(); |
} |
by: 发表于:2017-10-18 09:19:31 顶(0) | 踩(0) 回复
??
回复评论