编写一个books.xml文件
然后定义一个工具类MySax.java
import java.io.File;
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.helpers.DefaultHandler;
public class MySax extends DefaultHandler {
private String qName;
private boolean tag;
public static void main(String[] args) throws Exception {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
MySax m = new MySax();
parser.parse(new File("thinkmore/books.xml"),m);
}
public void startDocument() throws SAXException {
}
public void startElement(String uri,String localName,String qName,
Attributes attributes) throws SAXException {
if(qName.equals("book")){
System.out.print(attributes.getValue("isbn")+"\t");
}
this.qName=qName;
this.tag=true;
}
实现类TestSax.java
import java.io.File;
import java.util.LinkedList;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class TestSax extends DefaultHandler {
private String qName;
private boolean tag;
private LinkedList<Book> list= new LinkedList<Book>();
public static void main(String[] args) throws Exception {
SAXParserFactory factory=SAXParserFactory.newInstance();
SAXParser parser=factory.newSAXParser();
TestSax handler=new TestSax();
parser.parse(new File("thinkmore/books.xml"),handler);
}
@Override
public void startDocument() throws SAXException {
}
@Override
public void characters(char ch[],int start,int length)
throws SAXException {
String str=new String(ch,start,length);
if(this.qName.equals("price")&&tag){
System.out.print(str+"\t");
}
if(this.qName.equals("name")&&tag){
System.out.print(str+"\t");
}
if(this.qName.equals("author")&&tag){
System.out.print(str+"\t");
}
if(this.qName.equals("year")&&tag){
System.out.print(str+"\t");
}
}
@Override
public void startElement(String uri,
Attributes attributes) throws SAXException {
if(qName.equals("book")){
System.out.print(attributes.getValue("isbn")+"\t");
}
this.tag=true;
this.qName=qName;
}
@Override
public void endElement(String uri,String qName2)
throws SAXException {
if(qName2.equals("book")){
System.out.println();
}
this.tag=false;
}
@Override
public void endDocument() throws SAXException {
}
}
public void characters(char ch[],int length) throws SAXException { String str = new String(ch,length); if(this.qName.equals("name")){ System.out.print(str+"\t"); } if(this.qName.equals("price")){ System.out.print(str+"\t"); } if(this.qName.equals("author")){ System.out.print(str+"\t"); } if(this.qName.equals("year")){ System.out.print(str+"\t"); } } public void endElement(String uri,String qName) throws SAXException { if(this.qName.equals("book")){ System.out.println(); } this.tag=false; } public void endDocument() throws SAXException { } }
原文链接:https://www.f2er.com/xml/299997.html