TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
还是老样子,javabean实体类:
import java.io.*;
public class Book implements Serializable {
private int id;
private String name;
private double price;
public Book(){}
public Book(int id,String name,double price){
this.id=id;
this.name=name;
this.price=price;
}
public void setId(int id){
this.id=id;
}
public void setName(String name){
this.name=name;
}
public void setPrice(double price){
this.price=price;
}
}
[/code] 当然主要看这里:
public class SaxParseService extends DefaultHandler {
private List<Book> bookList;
private Book book;
private Stack<String> stack=new Stack<String>();
public List<Book> getBooks(InputStream inputStream,
SaxParseService saxParseService)
throws ParserConfigurationException, SAXException, IOException {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
saxParser.parse(inputStream, saxParseService);
return saxParseService.getBooks();
}
public List<Book> getBooks() {
return this.bookList;
}
@Override
public void startDocument() throws SAXException {
//***原样输出xml
System.out.println("<?xml version="1.0" encoding="UTF-8"?>");
//***原样输出xml
bookList = new ArrayList<Book>();
}
@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
super.endDocument();
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
//***原样输出xml
System.out.print("<"+qName);
if(attributes!=null&&attributes.getLength()>0){
for(int i=0;i<attributes.getLength();i++){
String attrName=attributes.getQName(i);
String attrValue=attributes.getValue(i);
System.out.print(" "+attrName+"="+"""+attrValue+""");
}
}
System.out.print(">");
//***原样输出xml
if ("book".equals(qName)) {
book = new Book();
book.setId(Integer.valueOf(attributes.getValue(0)));
}
stack.push(qName);
//System.out.println("startElement:"+stack.size());
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
//***原样输出xml
System.out.print("</"+qName+">");
//***原样输出xml
if ("book".equals(qName)) {
bookList.add(book);
book = null;
}
stack.pop();
//System.out.println("endElement:"+stack.size());
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
//***原样输出xml
System.out.print(new String(ch,start,length));
//***原样输出xml
String preTagName=stack.peek();
//System.out.println("characters:"+stack.size());
if (preTagName != null) {
String content = new String(ch, start, length);
if ("name".equals(preTagName)) {
book.setName(content);
} else if ("price".equals(preTagName)) {
book.setPrice(Double.valueOf(content));
}
}
}
} [/code] JUnit测试:
@Test
public void test() throws ParserConfigurationException, SAXException, IOException {
InputStream inputStream=this.getClass().getClassLoader().getResourceAsStream("xml.xml");
SaxParseService sps=new SaxParseService();
List<Book> bookList=sps.getBooks(inputStream,sps);
System.out.println("
");
for(Book book:bookList){
System.out.println(book);
}
}
[/code] 输出结果如下:
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book id="12">
<name>thinking in java</name>
<price>85.5</price>
</book>
<book id="15">
<name>Spring in Action</name>
<price>39.0</price>
</book>
</books>
12:thinking in java:85.5
15:Spring in Action:39.0 [/code] 代码中注释部分的输出是用来看stack实时size大小,有意思,你看出什么了吗:
startElement:1
characters:1
startElement:2
characters:2
startElement:3
characters:3
endElement:2
characters:2
startElement:3
characters:3
endElement:2
characters:2
endElement:1
characters:1
startElement:2
characters:2
startElement:3
characters:3
endElement:2
characters:2
startElement:3
characters:3
endElement:2
characters:2
endElement:1
characters:1
endElement:0
[/code]
源码下载:http://file.javaxxz.com/2014/10/9/235149250.zip |
|