//得到xml文檔所有內容package cn.itcast.sax;import java.io.IOException;import javax.xml.parsers.ParserConfigurationException;import javax.xml.parsers.SAXParser;import javax.xml.parsers.SAXParserFactory;import org.xml.sax.Attributes;import org.xml.sax.ContentHandler;import org.xml.sax.Locator;import org.xml.sax.SAXException;import org.xml.sax.XMLReader;public class test1 {/** * sax解析xml文檔 * @throws SAXException * @throws ParserConfigurationException * @throws IOException */public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {//1.建立解析工廠SAXParserFactory factory = SAXParserFactory.newInstance();//2.得到解析器SAXParser sp = factory.newSAXParser();//3.得到讀取器XMLReader reader = sp.getXMLReader();//4.設定內容處理器reader.setContentHandler(new ListHandler());//5.讀取xml文檔內容reader.parse("src/book.xml");}}//得到xml文檔所有內容class ListHandler implements ContentHandler{@Overridepublic void startElement(String uri, String localName, String qName,Attributes atts) throws SAXException {System.out.println("<" + qName + ">");for(int i = 0; atts != null && i < atts.getLength(); i++){String attName = atts.getQName(i);String attValue = atts.getValue(i);System.out.println(attName + "=" + attValue);}}@Overridepublic void characters(char[] ch, int start, int length)throws SAXException {System.out.println(new String(ch,start,length));}@Overridepublic void endElement(String uri, String localName, String qName)throws SAXException {System.out.println("</" + qName + ">");}@Overridepublic void setDocumentLocator(Locator locator) {// TODO Auto-generated method stub}@Overridepublic void startDocument() throws SAXException {// TODO Auto-generated method stub}@Overridepublic void endDocument() throws SAXException {// TODO Auto-generated method stub}@Overridepublic void startPrefixMapping(String prefix, String uri)throws SAXException {// TODO Auto-generated method stub}@Overridepublic void endPrefixMapping(String prefix) throws SAXException {// TODO Auto-generated method stub}@Overridepublic void ignorableWhitespace(char[] ch, int start, int length)throws SAXException {// TODO Auto-generated method stub}@Overridepublic void processingInstruction(String target, String data)throws SAXException {// TODO Auto-generated method stub}@Overridepublic void skippedEntity(String name) throws SAXException {// TODO Auto-generated method stub}}//擷取指定標籤的值package cn.itcast.sax;import java.io.IOException;import javax.xml.parsers.ParserConfigurationException;import javax.xml.parsers.SAXParser;import javax.xml.parsers.SAXParserFactory;import org.xml.sax.Attributes;import org.xml.sax.ContentHandler;import org.xml.sax.Locator;import org.xml.sax.SAXException;import org.xml.sax.XMLReader;import org.xml.sax.helpers.DefaultHandler;public class Text2 {/** * sax解析xml文檔 * @throws SAXException * @throws ParserConfigurationException * @throws IOException */public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {//1.建立解析工廠SAXParserFactory factory = SAXParserFactory.newInstance();//2.得到解析器SAXParser sp = factory.newSAXParser();//3.得到讀取器XMLReader reader = sp.getXMLReader();//4.設定內容處理器reader.setContentHandler(new TagValueHandler());//5.讀取xml文檔內容reader.parse("src/book.xml");}}//擷取指定標籤的值class TagValueHandler extends DefaultHandler{private String currentTag;//記住當前解析的是什麼標籤private int needNumber = 2;//記住想擷取第幾個作者標籤的值private int currentNumber;//當前解析的是第幾個@Overridepublic void startElement(String uri, String localName, String qName,Attributes attributes) throws SAXException {currentTag = qName;if(currentTag.equals("作者")){currentNumber++;}}@Overridepublic void characters(char[] ch, int start, int length)throws SAXException {if("作者".equals(currentTag) && currentNumber == needNumber){System.out.println(new String(ch,start,length));}}@Overridepublic void endElement(String uri, String localName, String qName)throws SAXException {currentTag = null;}}