sax解析xml文件,封裝到對象中
阿新 • • 發佈:2018-05-09
XP test xmlparse AI set version clas args java
創建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
sax解析xml文件,封裝到對象中