sax解析
2015-09-07 20:24
232 查看
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 Demo1 {
/**
*sax方式解析book1.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.利用解析器得到reader
XMLReader reader = sp.getXMLReader();
//4、在解析xml文档之前,设置好事件处理器
reader.setContentHandler(new MyContentHandler2());
//4.利用reader读取 xml文档
reader.parse("src/book1.xml");
}
}
//用于获取第一个售价节点的值:<售价>109</售价>
class MyContentHandler2 extends DefaultHandler{
private boolean isOk = false;
private int index = 1;
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if(isOk==true && index==1){
System.out.println(new String(ch,start,length));
}
}
@Override
public void startElement(String uri, String localName, String name,
Attributes attributes) throws SAXException {
if(name.equals("售价")){
isOk = true;
}
}
@Override
public void endElement(String uri, String localName, String name)
throws SAXException {
if(name.equals("售价")){
isOk = false;
index++;
}
}
}
//得到xml文档内容的事件处理器
class MyContentHandler implements ContentHandler{
public void startElement(String uri, String localName, String name,
Attributes atts) throws SAXException {
System.out.println("当前解析到了:" + name + ",这个标签是开始标签");
for(int i=0;i<atts.getLength();i++){
String attname = atts.getQName(i);
String attvalue = atts.getValue(i);
System.out.println(attname + "=" + attvalue);
}
}
public void endElement(String uri, String localName, String name)
throws SAXException {
System.out.println("当前解析到了:" + name + ",这个标签是结束标签");
}
public void characters(char[] ch, int start, int length)
throws SAXException {
System.out.println("当前解析到了内容:" + new String(ch,start,length));
}
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
}
public void endPrefixMapping(String prefix) throws SAXException {
// TODO Auto-generated method stub
}
public void ignorableWhitespace(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
}
public void processingInstruction(String target, String data)
throws SAXException {
// TODO Auto-generated method stub
}
public void setDocumentLocator(Locator locator) {
// TODO Auto-generated method stub
}
public void skippedEntity(String name) throws SAXException {
// TODO Auto-generated method stub
}
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
}
public void startPrefixMapping(String prefix, String uri)
throws SAXException {
// TODO Auto-generated method stub
}
}
<?xml version="1.0" encod
4000
ing="UTF-8"?>
<书架>
<书 name="yyyyyyy">
<售价>19元</售价>
<售价>19元</售价>
<书名>Java就业培训教程</书名>
<作者>张孝祥</作者>
<售价>209元</售价>
<售价>19元</售价>
<售价>19元</售价>
</书>
<书>
<书名>JavaScript网页开发</书名>
<作者>张孝祥</作者>
<售价>28.00元</售价>
</书>
</书架>
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 Demo1 {
/**
*sax方式解析book1.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.利用解析器得到reader
XMLReader reader = sp.getXMLReader();
//4、在解析xml文档之前,设置好事件处理器
reader.setContentHandler(new MyContentHandler2());
//4.利用reader读取 xml文档
reader.parse("src/book1.xml");
}
}
//用于获取第一个售价节点的值:<售价>109</售价>
class MyContentHandler2 extends DefaultHandler{
private boolean isOk = false;
private int index = 1;
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if(isOk==true && index==1){
System.out.println(new String(ch,start,length));
}
}
@Override
public void startElement(String uri, String localName, String name,
Attributes attributes) throws SAXException {
if(name.equals("售价")){
isOk = true;
}
}
@Override
public void endElement(String uri, String localName, String name)
throws SAXException {
if(name.equals("售价")){
isOk = false;
index++;
}
}
}
//得到xml文档内容的事件处理器
class MyContentHandler implements ContentHandler{
public void startElement(String uri, String localName, String name,
Attributes atts) throws SAXException {
System.out.println("当前解析到了:" + name + ",这个标签是开始标签");
for(int i=0;i<atts.getLength();i++){
String attname = atts.getQName(i);
String attvalue = atts.getValue(i);
System.out.println(attname + "=" + attvalue);
}
}
public void endElement(String uri, String localName, String name)
throws SAXException {
System.out.println("当前解析到了:" + name + ",这个标签是结束标签");
}
public void characters(char[] ch, int start, int length)
throws SAXException {
System.out.println("当前解析到了内容:" + new String(ch,start,length));
}
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
}
public void endPrefixMapping(String prefix) throws SAXException {
// TODO Auto-generated method stub
}
public void ignorableWhitespace(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
}
public void processingInstruction(String target, String data)
throws SAXException {
// TODO Auto-generated method stub
}
public void setDocumentLocator(Locator locator) {
// TODO Auto-generated method stub
}
public void skippedEntity(String name) throws SAXException {
// TODO Auto-generated method stub
}
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
}
public void startPrefixMapping(String prefix, String uri)
throws SAXException {
// TODO Auto-generated method stub
}
}
<?xml version="1.0" encod
4000
ing="UTF-8"?>
<书架>
<书 name="yyyyyyy">
<售价>19元</售价>
<售价>19元</售价>
<书名>Java就业培训教程</书名>
<作者>张孝祥</作者>
<售价>209元</售价>
<售价>19元</售价>
<售价>19元</售价>
</书>
<书>
<书名>JavaScript网页开发</书名>
<作者>张孝祥</作者>
<售价>28.00元</售价>
</书>
</书架>
相关文章推荐
- Codeforces 575H Bots 组合恒等式+逆元法求组合数取模
- Native Wifi 中 WlanSetProfile 出现1206错误的几种可能及解决方法
- Lua程序设计:第2章 类型和值
- java基础—5.异常处理
- MySQL学习----MySQL 算术运算符----06MySQL 算术运算符
- Firing (poj 2987 最大权闭合图)
- poj 1028 Web Navigation(模拟)
- C++ 之 explicit,mutable,volatile 浅析
- 键盘输入聊天
- java cpu占用过高分析
- 自我来扒一扒产品十宗罪
- 三子棋
- hdu 2011 多项式求和
- dom4j解析
- Mac 配置 Apache .php
- Android 自动适应字体大小的EditText
- UDP
- The Doors - POJ 1556 (线段相交)
- Vim 使用总结
- 在NeoKylin下安装QT5