<?xml version="1.0" encoding="UTF-8" standalone="no"?><books>
<book>
<name>三国演义</name>
<author>罗贯中</author>
<price>50rmb</price>
</book>
<book>
<name>水浒传</name>
<author>施耐庵</author>
<price>60rmb</price>
</book>
<book>
<name>红楼梦</name>
<author>曹雪芹</author>
<price>70rmb</price>
</book>
<book>
<name>西游记</name>
<author>吴承恩</author>
<price>80rmb</price>
</book>
</books>
//解析代码
package domdemo;
import java.util.ArrayList;
import java.util.List;
import javax.xml.namespace.QName;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
public class saxXMLparse {
public static void main(String[] args) throws Exception{
//第一种方法
SAXParser sax= SAXParserFactory.newInstance().newSAXParser();
sax.parse("src/demo1.xml",new DefaultHandler(){
Book book=null;
String node=null;
List<Book> books=new ArrayList<Book>();
@Override
public void startDocument() throws SAXException {
System.out.println("开始解析xml");
}
@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
System.out.println("xml解析结束,遍历books");
for(Book b:books){
System.out.println("书名"+b.getBookName());
System.out.println("作者"+b.getAuthor());
System.out.println("价格"+b.getPrice());
}
}
@Override
public void startElement(String uri,String localName,String qName,Attributes attributes)
throws SAXException {
if("book".equals("book")){
book=new Book();
}
node=qName;
System.out.println(node);
}
@Override
public void characters(char[] ch,int start,int length) throws SAXException {
System.out.println(new String(ch,start,length));
if("name".equals(node))
book.setBookName(new String(ch,length));
if("author".equals(node)){
System.out.println(node);
book.setAuthor(new String(ch,length));}
if("price".equals(node))
book.setPrice(Double.parseDouble(new String(ch,2)));
}
@Override
public void endElement(String uri,String qName) throws SAXException {
// TODO Auto-generated method stub
if("book".equals(qName)){
books.add(book);
book=null;
}
node=null;
}
});
//第二种方法
List<Book> books=new ArrayList<Book>();
SAXParser sax1= SAXParserFactory.newInstance().newSAXParser();
XMLReader reader=sax1.getXMLReader();
reader.setContentHandler(new DefaultHandler(){
Book book=null;
String node=null;
@Override
public void startDocument() throws SAXException {
System.out.println("开始解析xml");
}
@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
System.out.println("xml解析结束,遍历books");
}
@Override
public void startElement(String uri,String qName) throws SAXException {
// TODO Auto-generated method stub
if("book".equals(qName)){
books.add(book);
book=null;
}
node=null;
}
});
reader.parse("src/demo1.xml");//加载xml文件
for(Book b:books){//遍历
System.out.println("书名"+b.getBookName());
System.out.println("作者"+b.getAuthor());
System.out.println("价格"+b.getPrice());
}
}
}
//创建book类
class Book{
private String bookName;
private String author;
private double price;
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
原文链接:https://www.f2er.com/xml/294443.html