android開發基礎教程—三種方式實現xml檔案解析

來源:互聯網
上載者:User

1.sax方式 複製代碼 代碼如下:/**
* 使用sax解析
*/
public class SaxParse{
/**
* sax解析器
*/
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<Person> doParse(InputStream is) {
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> persons = null ;
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<Person>();
}
/**
* 元素開始事件
* localName:本地名
* uri:名字空間
* qName:限定名,首碼 + 本地名
*/
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<Person> getpersons(){
return persons ;
}
}
}

2.dom方式 複製代碼 代碼如下:/**
* 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<Person> doParse(InputStream is) {
List<Person> persons = new ArrayList<Person>();
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方式 複製代碼 代碼如下:/**
* pull解析,拉模式,可以手動控制下一個事件是否觸發.
*/
public class PullParse{
public List<Person> doParse(InputStream is) {
List<Person> persons = null ;
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<Person>();
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;
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.