1、XML文件userinfo.xml 可以放到assets文件夹下或者包下面
<?xml version="1.0" encoding="UTF-8"?> <admins> <admin id="1"> <name>于美光</name> <age>25</age> <address>NanJing</address> <phone>18855529592</phone> </admin> <admin id="2"> <name>于美光</name> <age>25</age> <address>NanJing</address> <phone>18855529592</phone> </admin> </admins>
2、新建一个与XML文件对应的类User.java
public class User { private int id; private String name; private String address; private int age; private String phone; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } }
3.工具类XmlParser 继承DefaultHandler
public class XmlParser extends DefaultHandler{ private String tagName = null; private User user = null; public User getUser() { return user; } public void setUser(User user) { this.user = user; } @Override public void startDocument() throws SAXException { user = new User(); } @Override public void startElement(String uri,String localName,String qName,Attributes attributes) throws SAXException { tagName = localName; if ("admin".equals(tagName)) { user.setId(Integer.parseInt(attributes.getValue(0))); } super.startElement(uri,localName,qName,attributes); } @Override public void characters(char[] ch,int start,int length) throws SAXException { if (tagName != null) { if (tagName.equals("name")) { String str = new String(ch,start,length); user.setName(str); }else if(tagName.equals("age")){ String str = new String(ch,length); user.setAge(Integer.parseInt(str)); }else if(tagName.equals("address")){ String str = new String(ch,length); user.setAddress(str); }else if(tagName.equals("phone")){ String str = new String(ch,length); user.setPhone(str); } } super.characters(ch,length); } @Override public void endElement(String uri,String qName) throws SAXException { tagName = null; super.endElement(uri,qName); } @Override public void endDocument() throws SAXException { super.endDocument(); } }
4.使用工具类进行解析
public class MainActivity extends Activity { private User user; private TextView txtUser; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); try { parse(); } catch (IOException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } txtUser = (TextView)findViewById(R.id.txtUser); txtUser.setText(Html.fromHtml("<b>ID:</b>"+user.getId()+"<b>NAME:</b>"+ "<b>AGE:</b>"+user.getAge()+"<b>Address:</b>"+user.getAddress()+ "<b>PHONE:</b>PHPNE"+"=+"+user.getPhone())); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main,menu); return true; } private void parse() throws IOException,ParserConfigurationException,SAXException{ SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); XmlParser parser = new XmlParser(); // InputStream is = this.getResources().getAssets().open("userinfo.xml");//资源文件夹下面的 InputStream is = this.getClass().getResourceAsStream("userinfo.xml");//和该类同一包下面 sp.parse(is,parser); user = parser.getUser(); } }