package net.csdn;
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.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
public class SaxDemo {
/**
* @param args
*/
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
//创建工厂
SAXParserFactory spf = SAXParserFactory.newInstance();
//创建sax解析器
SAXParser sp = spf.newSAXParser();
//获取读取器
XMLReader xmlReader = sp.getXMLReader();
//设置事件处理器
xmlReader.setContentHandler(new BookContenHandler1());
//
xmlReader.parse("src/book.xml");
}
}
class BookContenHandler1 extends DefaultHandler{
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
System.out.println("");
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
System.out.println(new String(ch,start,length));
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
System.out.println("");
}
}
使用sax解析获取第一个书名节点的值
最新推荐文章于 2021-02-28 08:33:32 发布