1一只小猫(标签中不带属性信息)
1
2
- <cats>
3
- <cat>
4
<id>1</id>
5
<name>波斯猫</name>
6
</cat>
7
- <cat>
8
<id>2</id>
9
<name>机器猫</name>
10
</cat>
11
- <cat>
12
<id>3</id>
13
<name>越野猫</name>
14
</cat>
15
- <cat>
16
<id>4</id>
17
<name>装甲猫</name>
18
</cat>
19
- <cat>
20
<id>5</id>
21
<name>超音速猫</name>
22
</cat>
23
</cats>
代码
1
public class Demo01 {
2
3
/**
4
* @param args
5
* @throws Exception
6
* @throws ParserConfigurationException
7
*/
8
public static void main(String[] args) throws ParserConfigurationException, Exception {
9
//1.创建解析器工厂
10
SAXParserFactory factory = SAXParserFactory.newInstance();
11
//2.利用工厂创建解析器的对象
12
SAXParser parser = factory.newSAXParser();
13
//3.解析xml
14
MyHandler dh = new MyHandler();
15
parser.parse("cat.xml", dh);
16
List<Cat> list = dh.getList();
17
for(Cat cat:list){
18
System.out.println(cat);
19
}
20
}
21
22
}
1
public class MyHandler extends DefaultHandler {
2
3
/**
4
* 用于保存解析结果的List
5
*/
6
private List<Cat> list;
7
8
9
public List<Cat> getList() {
10
return list;
11
}
12
13
/**
14
* 用于保存当前正在解析的标签
15
*/
16
private String tag;
17
/**
18
* 当前正在组装的cat对象
19
*/
20
private Cat cat;
21
22
23
public void startDocument() throws SAXException {
24
list = new ArrayList<Cat>();
25
}
26
27
28
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
29
tag = qName;//保存当前正在解析哪个标签
30
//qName:解析到的开始标签的标签名
31
if("cat".equals(qName)){//注意字符串必须与xml中的标签完全一样,否则解析不出来
32
cat = new Cat();
33
}
34
}
35
36
37
public void endElement(String uri, String localName, String qName) throws SAXException {
38
//qName::解析到的结束标签的标签名
39
tag = "";//为了防止tag没清空,读到空白文本,走characters时,将对象的属性值赋值成空白文本
40
if("cat".equals(qName)){//遇到了</cat>说明该对象组装完毕,可以加入list
41
list.add(cat);
42
}
43
}
44
45
46
public void characters(char[] ch, int start, int length) throws SAXException {
47
String content = new String(ch,start,length);
48
if("id".equals(tag)){//如果当前正在解析的是id
49
cat.setId(content);
50
}else if("name".equals(tag)){
51
cat.setName(content);
52
}
53
}
54
55
}
1
注意:不要写有参数的构造方法.因为是解析一个传一个值,不能一起传,写了还的再写一个无参的构造方法
2
public class Cat {
3
private String id;
4
private String name;
5
public String getId() {
6
return id;
7
}
8
public void setId(String id) {
9
this.id = id;
10
}
11
public String getName() {
12
return name;
13
}
14
public void setName(String name) {
15
this.name = name;
16
}
17
18
public String toString() {
19
return "Cat [id=" + id + ", name=" + name + "]";
20
}
21
22
}
2一个学生(标签不带属性信息)
1
2
- <students>
3
- <student>
4
<id>1</id>
5
<name>zhangsan</name>
6
<age>20</age>
7
<sex>male</sex>
8
</student>
9
- <student>
10
<id>2</id>
11
<name>lisi</name>
12
<age>21</age>
13
<sex>female</sex>
14
</student>
15
</students>
代码
1
/**用SAX解析student.xml
2
* @author Administrator
3
*
4
*/
5
public class Demo01Student {
6
7
public static void main(String[] args) throws Exception, SAXException {
8
SAXParserFactory factory = SAXParserFactory.newInstance();
9
SAXParser parser = factory.newSAXParser();
10
File file = new File("student.xml");
11
MyHandler mh = new MyHandler();
12
parser.parse(file, mh);
13
ArrayList<Student> list = mh.getList();
14
Iterator<Student> it = list.iterator();
15
while(it.hasNext()){
16
Student student = it.next();
17
System.out.println(student);
18
}
19
}
20
21
}
1
public class MyHandler extends DefaultHandler{
2
3
/**
4
* 承载着已经组装完毕的对象
5
*/
6
private ArrayList<Student> list;
7
8
public ArrayList<Student> getList() {
9
return list;
10
}
11
12
/**
13
* 当前正在解析的标签
14
*/
15
private String tag;
16
/**
17
* 当前正在组装的对象
18
*/
19
private Student student;
20
21
22
public void startDocument() throws SAXException {
23
list = new ArrayList<Student>();
24
}
25
26
27
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
28
tag = qName;
29
if("student".equals(qName)){
30
student = new Student();
31
}
32
}
33
34
35
public void endElement(String uri, String localName, String qName) throws SAXException {
36
tag = "";
37
if("student".equals(qName)){
38
list.add(student);
39
}
40
}
41
42
43
public void characters(char[] ch, int start, int length) throws SAXException {
44
String content = new String(ch,start,length);
45
if("id".equals(tag)){
46
student.setId(content);
47
}else if("name".equals(tag)){
48
student.setName(content);
49
}else if("age".equals(tag)){
50
student.setAge(Integer.parseInt(content));//String-->int
51
}else if("sex".equals(tag)){
52
student.setSex(content);
53
}
54
}
55
56
}
1
public class Student {
2
private String id;
3
private String name;
4
private int age;
5
private String sex;
6
7
public String toString() {
8
return "Student [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + "]";
9
}
10
public String getId() {
11
return id;
12
}
13
public void setId(String id) {
14
this.id = id;
15
}
16
public String getName() {
17
return name;
18
}
19
public void setName(String name) {
20
this.name = name;
21
}
22
public int getAge() {
23
return age;
24
}
25
public void setAge(int age) {
26
this.age = age;
27
}
28
public String getSex() {
29
return sex;
30
}
31
public void setSex(String sex) {
32
this.sex = sex;
33
}
34
35
}
第2个学生(标签中带有属性信息)
1
2
- <students>
3
- <student id="1">
4
<name>zhangsan</name>
5
<age>20</age>
6
<sex>male</sex>
7
</student>
8
- <student id="2">
9
<name>lisi</name>
10
<age>21</age>
11
<sex>female</sex>
12
</student>
13
</students>
代码
1
public class Demo02Student {
2
3
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
4
SAXParserFactory factory = SAXParserFactory.newInstance();
5
SAXParser parser = factory.newSAXParser();
6
File file = new File("student02.xml");
7
MyHandler mh = new MyHandler();
8
parser.parse(file, mh);
9
ArrayList<Student> list = mh.getList();
10
Iterator<Student> it = list.iterator();
11
while(it.hasNext()){
12
Student student = it.next();
13
System.out.println(student);
14
}
15
}
16
17
}
1
public class MyHandler extends DefaultHandler{
2
3
private ArrayList<Student> list;
4
private String tag;
5
private Student student;
6
7
8
public void startDocument() throws SAXException {
9
// TODO Auto-generated method stub
10
list = new ArrayList<Student>();
11
}
12
13
14
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
15
tag = qName;
16
if("student".equals(qName)){
17
student = new Student();
18
String value = attributes.getValue("id");//得到<student id="xxx">标签中id属性的值
19
student.setId(value);
20
}
21
}
22
23
24
public void endElement(String uri, String localName, String qName) throws SAXException {
25
tag = "";
26
if("student".equals(qName)){
27
list.add(student);
28
}
29
}
30
31
public ArrayList<Student> getList() {
32
return list;
33
}
34
35
36
public void characters(char[] ch, int start, int length) throws SAXException {
37
String content = new String(ch,start,length);
38
if("name".equals(tag)){
39
student.setName(content);
40
}else if("age".equals(tag)){
41
student.setAge(Integer.parseInt(content));
42
}else if("sex".equals(tag)){
43
student.setSex(content);
44
}
45
}
46
47
}
1
public class Student {
2
private String id;
3
private String name;
4
private int age;
5
private String sex;
6
7
public String toString() {
8
return "Student [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + "]";
9
}
10
public String getId() {
11
return id;
12
}
13
public void setId(String id) {
14
this.id = id;
15
}
16
public String getName() {
17
return name;
18
}
19
public void setName(String name) {
20
this.name = name;
21
}
22
public int getAge() {
23
return age;
24
}
25
public void setAge(int age) {
26
this.age = age;
27
}
28
public String getSex() {
29
return sex;
30
}
31
public void setSex(String sex) {
32
this.sex = sex;
33
}
34
35
}
一本书(标签中带有属性信息)
1
2
- <bookstore>
3
- <book category="java">
4
<title lang="english">java基础</title>
5
<author>gosling</author>
6
<year>2014</year>
7
<price>200</price>
8
</book>
9
- <book category="android">
10
<title lang="chinese">android基础</title>
11
<author>google</author>
12
<year>2014</year>
13
<price>20</price>
14
</book>
15
</bookstore>
代码
1
public class Demo04Book {
2
3
public static void main(String[] args) throws Exception {
4
// TODO Auto-generated method stub
5
SAXParserFactory factory = SAXParserFactory.newInstance();
6
SAXParser parser = factory.newSAXParser();
7
MyHandler mh = new MyHandler();
8
parser.parse("books.xml", mh);
9
ArrayList<Book> list = mh.getList();
10
for(Book b:list){
11
System.out.println(b);
12
}
13
}
14
15
}
1
public class MyHandler extends DefaultHandler{
2
3
private ArrayList<Book> list;
4
5
private String tag;
6
public ArrayList<Book> getList() {
7
return list;
8
}
9
10
private Book book;
11
12
13
public void startDocument() throws SAXException {
14
list = new ArrayList<Book>();
15
}
16
17
18
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
19
tag = qName;
20
if("book".equals(qName)){
21
book = new Book();
22
String value = attributes.getValue("category");
23
book.setCategory(value);
24
}else if("title".equals(qName)){//<title lang="english">
25
String value = attributes.getValue("lang");
26
book.setLang(value);
27
}
28
}
29
30
31
public void endElement(String uri, String localName, String qName) throws SAXException {
32
tag = "";
33
if("book".equals(qName)){
34
list.add(book);
35
}
36
}
37
38
39
public void characters(char[] ch, int start, int length) throws SAXException {
40
String content = new String(ch,start,length);
41
/*<title lang="english">java基础</title>
42
<author>gosling</author>
43
<year>2014</year>
44
<price>200</price>*/
45
if("title".equals(tag)){//解析"java基础"文本
46
book.setTitle(content);
47
}else if("author".equals(tag)){
48
book.setAuthor(content);
49
}else if("year".equals(tag)){
50
book.setYear(Integer.parseInt(content));
51
}else if("price".equals(tag)){
52
book.setPrice(Double.parseDouble(content));
53
}
54
}
55
56
}
1
public class Book {
2
/*<book category="java">
3
<title lang="english">java基础</title>
4
<author>gosling</author>
5
<year>2014</year>
6
<price>200</price>
7
</book>*/
8
private String category;
9
private String lang;
10
private String title;
11
private String author;
12
private int year;
13
private double price;
14
15
public String toString() {
16
return "Book [category=" + category + ", lang=" + lang + ", title=" + title + ", author=" + author + ", year="
17
+ year + ", price=" + price + "]";
18
}
19
public String getCategory() {
20
return category;
21
}
22
public void setCategory(String category) {
23
this.category = category;
24
}
25
public String getLang() {
26
return lang;
27
}
28
public void setLang(String lang) {
29
this.lang = lang;
30
}
31
public String getTitle() {
32
return title;
33
}
34
public void setTitle(String title) {
35
this.title = title;
36
}
37
public String getAuthor() {
38
return author;
39
}
40
public void setAuthor(String author) {
41
this.author = author;
42
}
43
public int getYear() {
44
return year;
45
}
46
public void setYear(int year) {
47
this.year = year;
48
}
49
public double getPrice() {
50
return price;
51
}
52
public void setPrice(double price) {
53
this.price = price;
54
}
55
56
}