book.xml
下面是利用javabean封装
<?xml version="1.0" encoding="UTF-8" standalone="no"?><书架>
<书 评价="好书啊">
<书名>Java就业培训教程</书名>
<作者>张孝祥</作者>
<售价>39.00元</售价>
</书>
<书>
<书名>JavaScript网页开发</书名>
<作者>黎活动</作者>
<售价>28.00元</售价>
</书>
</书架>
import javax.xml.parsers.ParserConfigurationException;
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 demo {
/**
* @param args
* @throws Exception
* @throws ParserConfigurationException
*/
public static void main(String[] args) throws ParserConfigurationException, Exception {
// TODO Auto-generated method stub
//创建工厂
SAXParserFactory factory=SAXParserFactory.newInstance();
//解析器
SAXParser parser=factory.newSAXParser();
//读取器
XMLReader reader=parser.getXMLReader();
//内容处理器
reader.setContentHandler(new Hander());
//读取xml内容
reader.parse("src/book.xml");
}
static class Hander extends DefaultHandler
{
@Override //startElement
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
System.out.println("<"+qName+">");
}
@Override//endElement
public void endElement(String uri, String localName, String qName)
throws SAXException {
System.out.println("<"+qName+">");
}
@Override//characters
public void characters(char[] ch, int start, int length)
throws SAXException {
System.out.println(new String(ch,start,length));
}
}
}
import javax.xml.parsers.ParserConfigurationException;
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 demo2 {
//得到作者
/**
* @param args
* @throws Exception
* @throws ParserConfigurationException
*/
public static void main(String[] args) throws ParserConfigurationException, Exception {
// TODO Auto-generated method stub
SAXParserFactory factory=SAXParserFactory.newInstance();
SAXParser parser=factory.newSAXParser();
XMLReader reader=parser.getXMLReader();
reader.setContentHandler(new Hander());
reader.parse("src/book.xml");
}
static class Hander extends DefaultHandler
{
private String markName;
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
markName=qName;
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if(markName.equals("作者"))
{
System.out.println(new String(ch,start,length));
}
}
}
}
下面是利用javabean封装
book.java
public class book {
private String name;
private String author;
private String price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
demo.java
import java.awt.List;
import java.util.ArrayList;
import javax.xml.parsers.ParserConfigurationException;
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 demo3 {
//javabean封装xml
/**
* @param args
* @throws Exception
* @throws ParserConfigurationException
*/
@SuppressWarnings("unused")
public static void main(String[] args) throws ParserConfigurationException, Exception {
// TODO Auto-generated method stub
SAXParserFactory factory=SAXParserFactory.newInstance();
SAXParser parser=factory.newSAXParser();
XMLReader reader=parser.getXMLReader();
Hander hand=new Hander();
reader.setContentHandler(hand);
reader.parse("src/book.xml");
ArrayList<book>list=hand.getList();
System.out.println(list);
}
static class Hander extends DefaultHandler
{
private ArrayList<book> list=new ArrayList<book>();
private String markName;
book b;
public ArrayList<book> getList()
{
return list;
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if(qName.equals("书"))
{
b=new book();
}
markName=qName;
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if(qName.equals("书"))
{
list.add(b);
b=null;
}
markName=null;
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if(("书名").equals(markName))
{
b.setName(new String(ch,start,length));
}
else if(("作者").equals(markName))
{
b.setAuthor(new String(ch,start,length));
}
else if(("售价").equals(markName))
{
b.setPrice(new String(ch,start,length));
}
}
}
}