java SAX使用范例
来源:互联网 发布:苹果7手机怎么备份数据 编辑:程序博客网 时间:2024/05/16 09:01
示例里使用的xml文件:books.xml
<?xml version="1.0" encoding="UTF-8"?>
<books>?
<book id="12">?
<name>thinking in java</name>?
<price>85.5</price>?
</book>?
<book id="15">?
<name>Spring in Action</name>?
<price>39.0</price>?
</book>?
</books>?
自定义Handler类:
package test;
import java.io.File;
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.helpers.DefaultHandler;
/**
* @author yuchao
*
*/
public class TestMyXmlHandler extends DefaultHandler {
private String currentValue = null;
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
super.startElement(uri, localName, qName, attributes);
if ("book".equals(qName)) {
System.out.println("book id=" + attributes.getValue("id"));
} else if ("name".equals(qName)) {
System.out.print("??? name : ");
}else if ("price".equals(qName)) {
System.out.print("??? price : ");
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
// TODO Auto-generated method stub
super.endElement(uri, localName, qName);
if (currentValue != null) {
System.out.println(currentValue);
currentValue = null;
}
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
super.characters(ch, start, length);
currentValue = new String(ch, start, length);
}
/**
* @param args
* @throws SAXException
* @throws ParserConfigurationException
* @throws IOException
*/
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
// TODO Auto-generated method stub
SAXParser parser=SAXParserFactory.newInstance().newSAXParser();
TestMyXmlHandler handler=new TestMyXmlHandler();
parser.parse(new File("conf/books.xml"),handler);
}
}
示例结果:
book id=12
name : thinking in java
price : 85.5
book id=15
name : Spring in Action
price : 39.0
<?xml version="1.0" encoding="UTF-8"?>
<books>?
<book id="12">?
<name>thinking in java</name>?
<price>85.5</price>?
</book>?
<book id="15">?
<name>Spring in Action</name>?
<price>39.0</price>?
</book>?
</books>?
自定义Handler类:
package test;
import java.io.File;
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.helpers.DefaultHandler;
/**
* @author yuchao
*
*/
public class TestMyXmlHandler extends DefaultHandler {
private String currentValue = null;
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
super.startElement(uri, localName, qName, attributes);
if ("book".equals(qName)) {
System.out.println("book id=" + attributes.getValue("id"));
} else if ("name".equals(qName)) {
System.out.print("??? name : ");
}else if ("price".equals(qName)) {
System.out.print("??? price : ");
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
// TODO Auto-generated method stub
super.endElement(uri, localName, qName);
if (currentValue != null) {
System.out.println(currentValue);
currentValue = null;
}
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
super.characters(ch, start, length);
currentValue = new String(ch, start, length);
}
/**
* @param args
* @throws SAXException
* @throws ParserConfigurationException
* @throws IOException
*/
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
// TODO Auto-generated method stub
SAXParser parser=SAXParserFactory.newInstance().newSAXParser();
TestMyXmlHandler handler=new TestMyXmlHandler();
parser.parse(new File("conf/books.xml"),handler);
}
}
示例结果:
book id=12
name : thinking in java
price : 85.5
book id=15
name : Spring in Action
price : 39.0
- java SAX使用范例
- 使用SAX技术对XML文档进行解析范例
- xml解析-sax范例
- java使用sax解析xml
- java使用sax解析xml
- java使用SAX解析XML
- Java XML解析 - 使用SAX 示例
- Java 使用SAX解析xml文件
- Java 使用SAX解析xml文件
- java中使用SAX解析XML
- 在java中使用sax解析xml
- Java 中使用 SAX 解析 XML 文档
- Java使用Sax解析xml文件
- JAVA-使用SAX解析XML数据
- java中使用SAX生成XML文件
- java使用SAX接口解析xml文件
- JAVA SAX
- java SAX
- Ubuntu下安装Xdebug
- IE自带的网页过渡特效
- WPF快速指导14:变换
- htmlParser解析提取html文件内容
- WPF快速指导15:动画
- java SAX使用范例
- IOS下布局兼容性问题解决
- 改善C#程序的建议6:在线程同步中使用信号量
- jquery提取表单内容备忘录
- 改善C#程序的建议7:正确停止线程
- 改变Scrum的每日站会(Daily Scrum)-译
- 【转】JSP/Servlet 获取真实IP地址
- php代码可移植性-符号
- 改善C#程序的建议8:避免锁定不恰当的同步对象