一、Dom4J解析
Dom4J解析代码
1 package dom4j; 2 3 import java.io.File; 4 import java.util.Iterator; 5 import java.util.List; 6 7 import org.dom4j.Attribute; 8 import org.dom4j.Document; 9 import org.dom4j.DocumentException;10 import org.dom4j.Element;11 import org.dom4j.io.SAXReader;12 13 public class testDom4J {14 15 public static void main(String[] args) {16 17 try {18 19 File file = new File("/home/ybxxszl/Java Workspace/test/src/user.xml");// 获取File对象20 SAXReader reader = new SAXReader();// 获取SAXReader对象21 Document document = reader.read(file);// 获取Document对象22 Element list = document.getRootElement();// 获取根节点23 Iteratorit1 = list.elementIterator();// 获取根节点下子节点的迭代器24 while (it1.hasNext()) {25 Element user = it1.next();// 获取根节点下子节点26 List userAttrs = user.attributes();// 获取根节点下子节点的属性名和属性值27 for (Attribute attribute : userAttrs) {28 System.out.println("属性名" + attribute.getName());29 System.out.println("属性值" + attribute.getValue());30 }31 Iterator it2 = user.elementIterator();// 获取根节点下子节点下子节点的迭代器32 while (it2.hasNext()) {33 Element userChild = it2.next();// 获取根节点下子节点下子节点34 System.out.println("节点名:" + userChild.getName());35 System.out.println("节点值:" + userChild.getStringValue());36 }37 }38 39 } catch (DocumentException e) {40 e.printStackTrace();41 }42 43 }44 45 }
二、SAX解析
User
1 package sax; 2 3 public class User { 4 5 private String id; 6 private String account; 7 private String password; 8 private String name; 9 10 public String getId() {11 return id;12 }13 14 public void setId(String id) {15 this.id = id;16 }17 18 public String getAccount() {19 return account;20 }21 22 public void setAccount(String account) {23 this.account = account;24 }25 26 public String getPassword() {27 return password;28 }29 30 public void setPassword(String password) {31 this.password = password;32 }33 34 public String getName() {35 return name;36 }37 38 public void setName(String name) {39 this.name = name;40 }41 42 @Override43 public String toString() {44 return "User [id=" + id + ", account=" + account + ", password=" + password + ", name=" + name + "]";45 }46 47 }
SAX解析代码
1 package sax; 2 3 import java.io.File; 4 import java.util.List; 5 6 import javax.xml.parsers.SAXParser; 7 import javax.xml.parsers.SAXParserFactory; 8 9 public class testSax {10 11 public static void main(String[] args) {12 13 try {14 15 File file = new File("/home/ybxxszl/Java Workspace/test/src/user.xml");16 SAXParserFactory factory = SAXParserFactory.newInstance();17 SAXParser parser = factory.newSAXParser();18 SAXParserHandler handler = new SAXParserHandler();19 parser.parse(file, handler);20 Listlist = handler.getList();21 for (int i = 0; i < list.size(); i++) {22 System.out.println(list.get(i).toString());23 }24 25 } catch (Exception e) {26 e.printStackTrace();27 }28 29 }30 31 }
1 package sax; 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 public class SAXParserHandler extends DefaultHandler {11 12 private String value = null;13 private User user = null;14 15 private Listlist = new ArrayList ();16 17 public List getList() {18 return list;19 }20 21 @Override22 public void startDocument() throws SAXException {23 super.startDocument();24 System.out.println("SAX解析开始");25 }26 27 @Override28 public void endDocument() throws SAXException {29 super.endDocument();30 System.out.println("SAX解析结束");31 }32 33 @Override34 public void startElement(String url, String localName, String qName, Attributes attributes) throws SAXException {35 super.startElement(url, localName, qName, attributes);36 if (qName.equals("user")) {37 user = new User();38 for (int i = 0; i < attributes.getLength(); i++) {39 System.out.println("属性名:" + attributes.getQName(i) + " 属性值:" + attributes.getValue(i));40 }41 } else if (!qName.equals("list") && !qName.equals("user")) {42 System.out.print("节点名:" + qName);43 }44 }45 46 @Override47 public void endElement(String uri, String localName, String qName) throws SAXException {48 super.endElement(uri, localName, qName);49 if (qName.equals("user")) {50 list.add(user);51 user = null;52 } else if (qName.equals("id")) {53 user.setId(value);54 } else if (qName.equals("account")) {55 user.setAccount(value);56 } else if (qName.equals("password")) {57 user.setPassword(value);58 } else if (qName.equals("name")) {59 user.setName(value);60 }61 }62 63 @Override64 public void characters(char[] c, int start, int length) throws SAXException {65 super.characters(c, start, length);66 value = new String(c, start, length);67 if (!value.trim().equals("")) {68 System.out.println(" 节点值:" + value);69 }70 }71 }
XML文件
1 23
4 91 5账号1 6密码1 7姓名1 810 152 11账号2 12密码2 13姓名2 1416 213 17账号3 18密码3 19姓名3 20