JAVA两种XML解析方式 SAX和DOM
Person实体类:
1 package com.icss.bean;
2
3 public class Person {
4 private int id;
5 private String name;
6 private int age;
7 public int getId() {
8 return id;
9 }
10 public void setId(int id) {
11 this.id = id;
12 }
13 public String getName() {
14 return name;
15 }
16 public void setName(String name) {
17 this.name = name;
18 }
19 public int getAge() {
20 return age;
21 }
22 public void setAge(int age) {
23 this.age = age;
24 }
25 public Person(int id, String name, int age) {
26 super();
27 this.id = id;
28 this.name = name;
29 this.age = age;
30 }
31 public Person() {
32 super();
33 }
34 @Override
35 public String toString() {
36 return "id="+id+" name="+name+" age="+age;
37 }
38
39
40
41 }
SAX解析方式 SAX帮助类:
1 package com.icss.biz;
2
3 import java.lang.reflect.Method;
4 import java.util.ArrayList;
5 import java.util.List;
6
7 import org.xml.sax.Attributes;
8 import org.xml.sax.SAXException;
9 import org.xml.sax.helpers.DefaultHandler;
10
11 import com.icss.bean.Person;
12
13 public class SaxHelper<T> extends DefaultHandler {
14 private List<Person> list;
15 private Person p;
16 String nameflag;
17
18
19 public List<Person> getList() {
20 return this.list;
21 }
22
23 @Override
24 public void startDocument() throws SAXException {
25 this.list = new ArrayList<Person>();
26 }
27
28 @Override
29 public void endDocument() throws SAXException {
30 }
31
32 @Override
33 public void startElement(String uri, String localName, String qName,
34 Attributes attributes) throws SAXException {
35 if (qName.equals("person")) {
36 p = new Person();
37 p.setId(new Integer(attributes.getValue("id")));
38 }
39 this.nameflag=qName;
40 }
41
42 @Override
43 public void endElement(String uri, String localName, String qName)
44 throws SAXException {
45 if (qName.equals("person")) {
46 if (p != null) {
47 list.add(p);
48 }
49 }
50 nameflag="";
51 }
52
53 @Override
54 public void characters(char[] ch, int start, int length)
55 throws SAXException {
56 if(nameflag.equals("name")){
57 p.setName(new String(ch,start,length));
58 }else if(nameflag.equals("age")){
59 p.setAge(new Integer(new String(ch,start,length)));
60 }
61
62 }
63
64
65 }
两种解析方法 业务类:
1 package com.icss.biz;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.util.ArrayList;
9 import java.util.List;
10
11 import javax.xml.parsers.DocumentBuilder;
12 import javax.xml.parsers.DocumentBuilderFactory;
13 import javax.xml.parsers.ParserConfigurationException;
14 import javax.xml.parsers.SAXParser;
15 import javax.xml.parsers.SAXParserFactory;
16
17 import org.w3c.dom.Document;
18 import org.w3c.dom.Element;
19 import org.w3c.dom.Node;
20 import org.w3c.dom.NodeList;
21 import org.xml.sax.SAXException;
22
23 import com.icss.bean.Person;
24
25 public class XMLParseBiz {
26 InputStream inputstream =null;
27
28 public XMLParseBiz(String xmlpath){
29 try {
30 File f = new File(xmlpath);
31 this.inputstream =new FileInputStream(f);
32 } catch (FileNotFoundException e) {
33 e.printStackTrace();
34 }
35 }
36 /**
37 * 通过DOM方法解析XML文件
38 * @param inputstream
39 * @return
40 * @throws ParserConfigurationException
41 * @throws IOException
42 * @throws SAXException
43 */
44 public List<Person> domParseXML( ) throws ParserConfigurationException, SAXException, IOException{
45 List<Person> list = new ArrayList<Person>();
46 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
47 DocumentBuilder builder = factory.newDocumentBuilder();
48 Document doc = builder.parse(this.inputstream);
49 Element root = doc.getDocumentElement();
50
51 NodeList personnodes =root.getElementsByTagName("person");
52 for(int i=0;i<personnodes.getLength();i++){
53 Element person = (Element)personnodes.item(i);
54 Person p = new Person();
55
56 p.setId(new Integer(person.getAttribute("id")));
57
58 NodeList nodelist =person.getChildNodes();
59 for(int j=0;j<nodelist.getLength();j++){
60 Node childnode = nodelist.item(j);
61 if(childnode.getNodeType()==Node.ELEMENT_NODE && childnode.getNodeName().equals("name")){
62 p.setName(childnode.getFirstChild().getNodeValue());
63 }else if(childnode.getNodeType()==Node.ELEMENT_NODE && childnode.getNodeName().equals("age")){
64 p.setAge(new Integer(childnode.getFirstChild().getNodeValue()));
65 }
66 }
67 list.add(p);
68 }
69 return list;
70 }
71
72 /**
73 * 通过SAX方法解析XML文件
74 * @param inputstream
75 * @return
76 * @throws IOException
77 * @throws SAXException
78 * @throws ParserConfigurationException
79 */
80 public List<Person> saxParseXML( ) throws SAXException, IOException, ParserConfigurationException{
81 SAXParserFactory factory = SAXParserFactory.newInstance();
82 SAXParser parser =factory.newSAXParser();
83
84 SaxHelper helper = new SaxHelper();
85 parser.parse(this.inputstream, helper);
86
87 return helper.getList();
88 }
89 }
宁愿跑起来被拌倒无数次,也不愿规规矩矩走一辈子。就算跌倒也要豪迈的笑!
浙公网安备 33010602011771号