sax解析

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" encoding="UTF-8"?>


<书架> 
  <书 name="yyyyyyy"> 
    <售价>19元</售价>  
    <售价>19元</售价>
    <书名>Java就业培训教程</书名>  
    <作者>张孝祥</作者>  
    <售价>209元</售价>  
    <售价>19元</售价>  
    <售价>19元</售价> 
  </书>  
  <书> 
    <书名>JavaScript网页开发</书名>  
    <作者>张孝祥</作者>  
    <售价>28.00元</售价> 
  </书> 
</书架>









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值