您的位置:首页 > 其它

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元</售价> 

  </书> 

</书架>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: