创建User.java类
public class User { private String id; private String name; private String age; private String password; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
xml文件内容
<?xml version="1.0" encoding="UTF-8"?> <users> <user id="A001"> <name>Jack</name> <age>25</age> </user> <user id="A002"> <name>张三</name> <age>80</age> </user> </users>
创建XmlParseHandler.java类。集成DefaultHandler,重写部分方法
import java.util.ArrayList; import java.util.List; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class XmlParseHandler extends DefaultHandler { private String value = null; private List<User> userList = new ArrayList<User>(); private User user = null; @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { super.startElement(uri, localName, qName, attributes); if ("user".equals(qName)) { user = new User(); String id = attributes.getValue("id"); user.setId(id); } } @Override public void characters(char[] ch, int start, int length) throws SAXException { super.characters(ch, start, length); value = new String(ch, start, length); } @Override public void endElement(String uri, String localName, String qName) throws SAXException { super.endElement(uri, localName, qName); if (qName.equals("user")) { userList.add(user); user = null; } if (qName.equals("name")) { user.setName(value); } if (qName.equals("age")) { user.setAge(value); } } public List<User> getUserList() { return userList; } public void setUserList(List<User> userList) { this.userList = userList; } }
创建XmlParseUtils.java类
import java.util.ArrayList; import java.util.List; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; public class XmlParseUtils { public static List<User> getUser(String xml) throws Exception { List<User> list=new ArrayList<User>(); // 创建SAXParserFactory实例 SAXParserFactory spf = SAXParserFactory.newInstance(); try { // 创建SAXParser实例 SAXParser parser = spf.newSAXParser(); // 创建DefaultHandler实例 XmlParseHandler handler = new XmlParseHandler(); // 开始解析 parser.parse(xml, handler); list=handler.getUserList(); } catch (Exception e) { e.printStackTrace(); } return list; } }
创建测试类
import java.util.List; public class TestSax { public static void main(String[] args) { try { List<User> list=XmlParseUtils.getUser("E:/app/users.xml"); for(User ele:list){ System.out.println("name:"+ele.getName()); System.out.println("age:"+ele.getAge()); } } catch (Exception e) { e.printStackTrace(); } System.exit(0); } }
控制台输出结果
name:Jack age:25 name:张三 age:80
原文地址:https://www.cnblogs.com/l412382979/p/9012357.html
时间: 2024-10-10 07:46:12