xml解析

这是解析的代码:

 

package com.icss.test_sax_xmlfile.biz;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.util.Log;
import android.util.Xml;

import com.icss.test_sax_xmlfile.StudentHandler;
import com.icss.test_sax_xmlfile.bean.Student;

public class StudentBiz {
 public static List<Student> getlistFromSDBySAX(InputStream inputstream) throws IOException, SAXException, ParserConfigurationException{
  List<Student> list=null;
  //业务
  //创建sax解析器工厂
  SAXParserFactory spf=SAXParserFactory.newInstance();
  //通过解析器工厂拿到解析器
  SAXParser sp=spf.newSAXParser();
  //解析器加流及handler来解析
  StudentHandler sxh=new StudentHandler();
  sp.parse(inputstream, sxh);
  //handler中取出list
  list=sxh.getList();
     //关闭流
  inputstream.close();
  return list;
 }
public static List<Student> getListFromSDCardByPull(InputStream inputStream)
  throws ParserConfigurationException, SAXException, IOException,
  XmlPullParserException {
   List<Student> list = null;
   Student s = null;
   // 一种,直接pull
   // XmlPullParserFactory f=XmlPullParserFactory.newInstance();
   // XmlPullParser p=f.newPullParser(); //pull解析器
   // 二种, android对pull的支持
   XmlPullParser p = Xml.newPullParser(); // 通过android的pull支持创建一个pull解析器
   p.setInput(inputStream, "utf-8");
   int eventType = p.getEventType();

   while (eventType != XmlPullParser.END_DOCUMENT) {
    Log.i("localName", "" + p.getName());
    // 每一个节点业务
    switch (eventType) {
    case XmlPullParser.START_DOCUMENT:
     list = new ArrayList<Student>();

     break;
    case XmlPullParser.START_TAG:
     if ("student".equals(p.getName())) {
      s = new Student();
      s.setId(new Integer(p.getAttributeValue(null, "id")));
     } else if ("name".equals(p.getName())) {
      if (s != null) {
       s.setName(p.nextText());
      }
     } else if ("age".equals(p.getName())) {
      if (s != null) {
       s.setAge(new Integer(p.nextText()));
      }
     }
     break;
    case XmlPullParser.END_TAG:
     if ("student".equals(p.getName()) && s != null) {
      list.add(s);
     }
     break;
    }
    eventType = p.next(); // 到下一个节点
   }

   return list;
  }
}

 

 

实体类

 

package com.icss.test_sax_xmlfile.bean;

public class Student {
   private int id;
   private String name;
   private int age;
  
  
  
  
public int getId() {
 return id;
}
@Override
public String toString() {
 return "年龄" + age + ", id=" + id + ", 名字" + name ;
}
public void setId(int id) {
 this.id = id;
}
public String getName() {
 return name;
}
public void setName(String name) {
 this.name = name;
}
public int getAge() {
 return age;
}
public void setAge(int age) {
 this.age = age;
}
public Student(int id, String name, int age) {
 super();
 this.id = id;
 this.name = name;
 this.age = age;
}
public Student() {
 super();
}
}

 

 

Halder

 

package com.icss.test_sax_xmlfile;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

import com.icss.test_sax_xmlfile.bean.Student;

public class StudentHandler extends DefaultHandler {
 private List<Student> list;
 private Student s;
 private String preTag;
 
 public List<Student> getList(){
  return this.list;
 }
 
 @Override
 public void startDocument() throws SAXException {
  list=new ArrayList<Student>();
  super.startDocument();  
 }
 
 @Override
 public void startElement(String uri, String localName, String qName,
   Attributes attributes) throws SAXException {
  Log.i("localName", localName);
  if(localName!=null&&localName.equals("student")){
   s=new Student();
      s.setId(Integer.parseInt(attributes.getValue("id")));
  }
  preTag=localName;
  super.startElement(uri, localName, qName, attributes);
 }
 

 @Override
 public void characters(char[] ch, int start, int length)
   throws SAXException {
  if(s!=null){
   if(preTag!=null&&preTag.equals("name")){
    s.setName(new String(ch,start,length));
   }else if(preTag!=null&&preTag.equals("age")){
    s.setAge(Integer.parseInt(new String(ch,start,length)));
   }
  }
  super.characters(ch, start, length);
 }

 @Override
 public void endElement(String uri, String localName, String qName)  // name  age   student
   throws SAXException {
  if(localName!=null&&localName.equals("student")){
   list.add(s);
  }
 
  preTag="";
  Log.i("localName",list.size()+"");
  super.endElement(uri, localName, qName);
 }

  
}

 

 

MainActivity

package com.icss.test_sax_xmlfile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.List;

import com.icss.test_sax_xmlfile.bean.Student;
import com.icss.test_sax_xmlfile.biz.StudentBiz;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.widget.Toast;

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
        File f=new File(Environment.getExternalStorageDirectory(),"ysj.xml");
        FileInputStream fis=null;
  try {
   fis = new FileInputStream(f);
   } catch (FileNotFoundException e) {
   e.printStackTrace();
  }
   List<Student> list=null;
   try {
   list=StudentBiz.getlistFromSDBySAX(fis);
  } catch (Exception e) {
   Toast.makeText(this, "出错了"+e.toString(), Toast.LENGTH_LONG).show();
  }
  Toast.makeText(this, list.toString(), Toast.LENGTH_LONG).show();
        }
    }
}

 

你可能感兴趣的:(xml,android)