样例,解析器:
1 package com.mogu.parse; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import org.xml.sax.Attributes; 7 import org.xml.sax.SAXException; 8 import org.xml.sax.helpers.DefaultHandler; 9 10 import com.mogu.dto.User; 11 12 import android.util.Log; 13 14 public class MySAXHandler extends DefaultHandler { 15 16 private String tagName; 17 private StringBuffer sBuffer; 18 private List<User> userList; 19 private User user; 20 21 public List<User> getUserList() { 22 return userList; 23 } 24 25 @Override 26 public void startDocument() throws SAXException { 27 userList = new ArrayList<User>(); 28 sBuffer = new StringBuffer(); 29 Log.d("FirstSAX", "start parse xml file"); 30 } 31 32 @Override 33 public void endDocument() throws SAXException { 34 Log.d("FirstSAX", "parse xml file end"); 35 } 36 37 @Override 38 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { 39 tagName = localName; 40 sBuffer.setLength(0); 41 if ("user".equals(localName)) { 42 user = new User(); 43 user.setId(Integer.parseInt(attributes.getValue("id"))); 44 } 45 } 46 47 @Override 48 public void endElement(String uri, String localName, String qName) throws SAXException { 49 if ("user".equals(localName)) { 50 userList.add(user); 51 user = null; 52 } else if ("name".equals(localName)) { 53 user.setName(sBuffer.toString()); 54 } else if ("age".equals(localName)){ 55 user.setAge(Integer.parseInt(sBuffer.toString())); 56 } 57 tagName = null; 58 } 59 60 @Override 61 public void characters(char[] ch, int start, int length) throws SAXException { 62 if (tagName != null) { 63 sBuffer.append(new String(ch, start, length)); 64 } 65 } 66 67 }
实体类:
1 package com.mogu.dto; 2 3 public class User { 4 5 private int id; 6 private String name; 7 private int age; 8 9 public int getId() { 10 return id; 11 } 12 public void setId(int id) { 13 this.id = id; 14 } 15 public String getName() { 16 return name; 17 } 18 public void setName(String name) { 19 this.name = name; 20 } 21 public int getAge() { 22 return age; 23 } 24 public void setAge(int age) { 25 this.age = age; 26 } 27 28 @Override 29 public String toString() { 30 return "姓名:" + name +";年龄:" + age; 31 } 32 33 }
activity:
1 package com.mogu.firstsax; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.util.List; 6 7 import javax.xml.parsers.ParserConfigurationException; 8 import javax.xml.parsers.SAXParser; 9 import javax.xml.parsers.SAXParserFactory; 10 11 import org.xml.sax.SAXException; 12 13 import com.lidroid.xutils.ViewUtils; 14 import com.lidroid.xutils.view.annotation.ViewInject; 15 import com.lidroid.xutils.view.annotation.event.OnClick; 16 import com.mogu.dto.User; 17 import com.mogu.parse.MySAXHandler; 18 19 import android.app.Activity; 20 import android.os.Bundle; 21 import android.view.Menu; 22 import android.view.MenuItem; 23 import android.view.View; 24 import android.view.Window; 25 import android.view.WindowManager; 26 import android.widget.ArrayAdapter; 27 import android.widget.Button; 28 import android.widget.ListView; 29 30 public class MainActivity extends Activity { 31 32 @ViewInject(R.id.btn_parse) 33 private Button btn_parse; 34 35 @ViewInject(R.id.lv_main) 36 private ListView lv_main; 37 38 @Override 39 protected void onCreate(Bundle savedInstanceState) { 40 super.onCreate(savedInstanceState); 41 // 设置无标题 42 requestWindowFeature(Window.FEATURE_NO_TITLE); 43 // 设置全屏 44 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 45 setContentView(R.layout.activity_main); 46 ViewUtils.inject(this); 47 } 48 49 @Override 50 public boolean onCreateOptionsMenu(Menu menu) { 51 // Inflate the menu; this adds items to the action bar if it is present. 52 getMenuInflater().inflate(R.menu.main, menu); 53 return true; 54 } 55 56 @Override 57 public boolean onOptionsItemSelected(MenuItem item) { 58 // Handle action bar item clicks here. The action bar will 59 // automatically handle clicks on the Home/Up button, so long 60 // as you specify a parent activity in AndroidManifest.xml. 61 int id = item.getItemId(); 62 if (id == R.id.action_settings) { 63 return true; 64 } 65 return super.onOptionsItemSelected(item); 66 } 67 68 @OnClick(R.id.btn_parse) 69 private void startParse(View v) { 70 InputStream file = null; 71 try { 72 file = getAssets().open("user.xml"); 73 try { 74 // 创建一个SAXParserFactory 75 SAXParserFactory factory = SAXParserFactory.newInstance(); 76 // ②创建SAX解析器 77 SAXParser parser = factory.newSAXParser(); 78 MySAXHandler mySAXHandler = new MySAXHandler(); 79 parser.parse(file, mySAXHandler); 80 List<User> userList = mySAXHandler.getUserList(); 81 System.out.println("userList:" + userList); 82 ArrayAdapter<User> adapter = new ArrayAdapter<User>(MainActivity.this, 83 android.R.layout.simple_expandable_list_item_1, userList); 84 lv_main.setAdapter(adapter); 85 } catch (SAXException | ParserConfigurationException e) { 86 e.printStackTrace(); 87 } 88 // List<User> 89 } catch (IOException e) { 90 e.printStackTrace(); 91 } finally { 92 if (file != null) { 93 try { 94 file.close(); 95 } catch (IOException e) { 96 e.printStackTrace(); 97 } 98 } 99 } 100 } 101 }
被解析的xml文件:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <users> 3 <user id="1"> 4 <name>jack</name> 5 <age>20</age> 6 </user> 7 <user id="2"> 8 <name>lucy</name> 9 <age>21</age> 10 </user> 11 </users>