java使用builder设计模式解析这段xml:<?xml version="1.0" encoding="UTF-8"?><bookstore> <book category="children"> <title lang="en">Harry Potter</title> <author>J.K. Rowling</author> <year>2005</year> <price>29.99</price> </book> <book category="web"> <title lang="en">Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book></bookstore>
时间: 2023-06-26 16:07:16 浏览: 54
首先,我们需要定义一个Book类来表示XML中的每个书籍。这个类需要有四个属性:category、title、author和price。
然后,我们可以定义一个BookBuilder类来构建Book对象。BookBuilder类应该具有以下方法:
1. setCategory(String category):设置book的category属性。
2. setTitle(String title):设置book的title属性。
3. setAuthor(String author):设置book的author属性。
4. setYear(int year):设置book的year属性。
5. setPrice(double price):设置book的price属性。
6. build():构建一个Book对象并返回。
接下来,我们可以定义一个Bookstore类来表示整个XML文档。这个类应该具有一个List<Book>属性来存储所有的书籍。
最后,我们需要创建一个BookstoreBuilder类来解析XML文档并构建Bookstore对象。BookstoreBuilder类应该具有以下方法:
1. parseDocument(InputStream inputStream):解析XML文档并返回一个Bookstore对象。
下面是完整的代码实现:
Book.java
```
public class Book {
private String category;
private String title;
private String author;
private int year;
private double price;
public Book(String category, String title, String author, int year, double price) {
this.category = category;
this.title = title;
this.author = author;
this.year = year;
this.price = price;
}
// getters and setters
}
```
BookBuilder.java
```
public class BookBuilder {
private String category;
private String title;
private String author;
private int year;
private double price;
public BookBuilder setCategory(String category) {
this.category = category;
return this;
}
public BookBuilder setTitle(String title) {
this.title = title;
return this;
}
public BookBuilder setAuthor(String author) {
this.author = author;
return this;
}
public BookBuilder setYear(int year) {
this.year = year;
return this;
}
public BookBuilder setPrice(double price) {
this.price = price;
return this;
}
public Book build() {
return new Book(category, title, author, year, price);
}
}
```
Bookstore.java
```
import java.util.ArrayList;
import java.util.List;
public class Bookstore {
private List<Book> books;
public Bookstore() {
books = new ArrayList<>();
}
public void addBook(Book book) {
books.add(book);
}
// getters and setters
}
```
BookstoreBuilder.java
```
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;
import java.io.InputStream;
public class BookstoreBuilder {
public Bookstore parseDocument(InputStream inputStream) {
Bookstore bookstore = new Bookstore();
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
DefaultHandler handler = new DefaultHandler() {
private BookBuilder bookBuilder;
private String currentElement;
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
switch (qName) {
case "book":
bookBuilder = new BookBuilder();
bookBuilder.setCategory(attributes.getValue("category"));
break;
case "title":
case "author":
case "year":
case "price":
currentElement = qName;
break;
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
switch (qName) {
case "book":
bookstore.addBook(bookBuilder.build());
bookBuilder = null;
break;
case "title":
case "author":
case "year":
case "price":
currentElement = null;
break;
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
if (currentElement != null) {
String value = new String(ch, start, length);
switch (currentElement) {
case "title":
bookBuilder.setTitle(value);
break;
case "author":
bookBuilder.setAuthor(value);
break;
case "year":
bookBuilder.setYear(Integer.parseInt(value));
break;
case "price":
bookBuilder.setPrice(Double.parseDouble(value));
break;
}
}
}
};
saxParser.parse(inputStream, handler);
} catch (ParserConfigurationException | SAXException | IOException e) {
e.printStackTrace();
}
return bookstore;
}
}
```
现在我们可以使用以下代码来解析XML文档并获取所有的书籍:
```
InputStream inputStream = new FileInputStream("bookstore.xml");
BookstoreBuilder bookstoreBuilder = new BookstoreBuilder();
Bookstore bookstore = bookstoreBuilder.parseDocument(inputStream);
List<Book> books = bookstore.getBooks();
for (Book book : books) {
System.out.println(book.getTitle() + " by " + book.getAuthor());
}
```