Sax解析xml文档
测试的xml数据:
<?xml version="1.0" encoding="utf-8" ?>
<note>
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note>
新建SaxTest类继承至DefaultHandle
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler; import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import java.io.FileInputStream;
import java.io.IOException; public class SaxTest extends DefaultHandler {
@Override
public void startDocument() throws SAXException {
System.out.println("开始解析文档");
} @Override
public void endDocument() throws SAXException {
System.out.println("解析文档结束");
} @Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
System.out.println(qName);//每个标签开始都会触发该事件,attributes是标签的属性
} @Override
public void endElement(String uri, String localName, String qName) throws SAXException {
System.out.println(qName);//每个标签结束都会触发该事件
} @Override
public void characters(char[] ch, int start, int length) throws SAXException {
System.out.println(new String(ch, start, length));//标签开始到标签结束之间的内容
} static public void main(String[] args) throws IOException, ParserConfigurationException, SAXException {
String textXml = SaxTest.class.getResource("/").getPath() + "test.xml";
FileInputStream inputStream = new FileInputStream(textXml);
SAXParserFactory.newInstance().newSAXParser().parse(inputStream, new SaxTest());
}
}
解析结果截图:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAATcAAAIoCAIAAACDKDu2AAAUJElEQVR4nO3dTW7j2HqA4budOhoFQYBUECAI0AR6A11Dc9ILSA0CztLzVOGs4GbGTdzDzWVASab4Z5GSVZ+t58HFLZumKMr2q0Na4um//ft//Of5f//0z/9y/t8ff/xxuPRvE1+//v7nXz9+/vz58+fP79++nhZ++/7j+7evX2dW/vPb16/fvv/48fPnzx+ndaZLjuv//udfP378+PHXn78fF377fryvHz++//Gvf8zf6tv3nyf/+z//9d8/vn877s/8Xs3u8DVLDvAof7ul0q1+//Ovc8xbff367fup2KUngkf6JT8tntNDK73R73/+dR4nd9d+L7/kp8Vz+kiVhvJLflo8J5Xu9Et+WjynDZU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CqF6FQK0akUolMpRKdSiE6lEJ1KITqVQnQqhehUCtGpFKJTKUS3odJcyvm/aJbr9Et2dyrVud+lUnKdUkp1LrlOa7u3vk5KdS6lvLWRHSvf/d5vvK97bHzDrqZUNW3puq60TfU+u/RZbaj0/CNPVdO+289+k5TqXNqmuvOebPrlvr2E0RbeP627bXzT1qqm7Z/cU9XkprrLDjyJT1Dp/ffk0ZVefj/ft9K7/uyu39WU+kjT8VaG0y22VlrOR7yvBzxV0w6OfNLx09chrl/SdV3J9XDJcJ06H7d8PhyaLlnZn8Hh7mBPTp/mPLNw+Ls1uq/+ly/n0nVde3rWn3lclwfbc7vXNlWdS8l1qppmuofDR3G5h+N7n3n4g/05bjbXafCzuOa+Vra8/vOaPvbpOpPvxsUDjPAs/1HsGUsvl5RhRSlVTds21eu/ryPv6dl08LXjc+rClt/+Qb55w34PT88p878lS49reKu5fR4sWdjVOpdc17mUtm1ff+N37fPkgc/uT/+E9OZDW0105ed18TO9eOzT/Vn5Sal0q9srnV8y/eBynTJ+Jj4+8Q+erSdLpq77jR/vxuLTzem+Fh7FdNx+4+9VVdO2bdvf6nVM3rXPcw988j2smvby27Wr0g0/05Xvz2Szjnj3u3uli2Pp4XCo6vq0ZLydpqkOl78QoyWze3+XSmfvffLBdJ8Hj3SppTp3Xdfm3JZy/qv4WgZ1fWX/C/uzYSzt72thy9Of18JYeh53J/sz9frXozqvHMkztfmVmNHoN33uXDmHGZ3jDc4Dj3+jP58LTZdMLZ2XLnx6PEl7+fIyeSAX9zV7q9PfzC5eSOgfV2nbdnKiO3iY/anpcJSe+6adTywX7n3m4Q/257cvL8djgXp8XrpyX0u/E0s/r+nPdPjYp9+fyc/LKzE7eVcDRKdSiE6lEJ1KITqVQnQqhehUCtGpFKJTKUSnUohOpRCdSiE6lUJ0KoXoVArRqRSiUylEp1KITqUQ3f8DmmwC9Gm2xmsAAAAASUVORK5CYII=" alt="" />
其中characters方法需要特别注意,开始标签到结束标签的内容可能不止会触发一次characters事件,也就是说标签中的内容可能会多次触发characters事件,所以如果要获取标签内的完整内容需要特别小心,一般使用StringBuilder,如下代码。
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler; import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import java.io.FileInputStream;
import java.io.IOException; public class SaxTest extends DefaultHandler {
StringBuilder stringBuilder = new StringBuilder(); @Override
public void startDocument() throws SAXException {
System.out.println("开始解析文档");
} @Override
public void endDocument() throws SAXException {
System.out.println("解析文档结束");
} @Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
System.out.println(qName);//每个标签开始都会触发该事件,attributes是标签的属性
} @Override
public void endElement(String uri, String localName, String qName) throws SAXException {
System.out.println(stringBuilder.toString().trim());
stringBuilder.delete(0, stringBuilder.length());//输出内容,并清空stringBuilder System.out.println(qName);//每个标签结束都会触发该事件
} @Override
public void characters(char[] ch, int start, int length) throws SAXException {
stringBuilder.append(ch, start, length);
} static public void main(String[] args) throws IOException, ParserConfigurationException, SAXException {
String textXml = SaxTest.class.getResource("/").getPath() + "test.xml";
FileInputStream inputStream = new FileInputStream(textXml);
SAXParserFactory.newInstance().newSAXParser().parse(inputStream, new SaxTest());
}
}
Sax解析xml文档的更多相关文章
- 使用SAX解析xml文档
1.首先,在main方法中解析xml文档,具体代码如下: import org.w3c.dom.*; import javax.xml.parsers.DocumentBuilder; import ...
- SAX解析XML文档——(二)
SAX从上向下解析,一行一行解析.节省内存,不适合CRUD. XML文档: <?xml version="1.0" encoding="UTF-8"?&g ...
- Java高级特性 第14节 解析XML文档(2) - SAX 技术
一.SAX解析XML文档 SAX的全称是Simple APIs for XML,也即XML简单应用程序接口.与DOM不同,SAX提供的访问模式是一种顺序模式,这是一种快速读写XML数据的方式.当使用S ...
- 四种生成和解析XML文档的方法详解(介绍+优缺点比较+示例)
众所周知,现在解析XML的方法越来越多,但主流的方法也就四种,即:DOM.SAX.JDOM和DOM4J 下面首先给出这四种方法的jar包下载地址 DOM:在现在的Java JDK里都自带了,在xml- ...
- 浅谈用java解析xml文档(二)
上一文中总结了dom解析xml文档的方式,本文开始总结使用SAX解析xml 的方式及它的优缺点! SAX(Simple API for XML),是指一种接口,或者一个软件包. 首先我们应该知道SAX ...
- 四种生成和解析XML文档的方法详解
众所周知,现在解析XML的方法越来越多,但主流的方法也就四种,即:DOM.SAX.JDOM和DOM4J 下面首先给出这四种方法的jar包下载地址 DOM:在现在的Java JDK里都自带了,在xml- ...
- Java高级特性 第13节 解析XML文档(1) - DOM和XPath技术
一.使用DOM解析XML文档 DOM的全称是Document Object Model,也即文档对象模型.在应用程序中,基于DOM的XML分析器将一个XML文档转换成一个对象模型的集合(通常称DOM树 ...
- 大杂烩 -- 四种生成和解析XML文档的方法详解
基础大杂烩 -- 目录 众所周知,现在解析XML的方法越来越多,但主流的方法也就四种,即:DOM.SAX.JDOM和DOM4J DOM:在现在的Java JDK里都自带了,在xml-apis.jar包 ...
- 使用dom4j解析XML文档
dom4j的包开源包,不属于JDK里面,在myeclipse中要单独导入在项目中,这里不累赘了 做这个过程,很慢,因为很多方法没用过不熟悉,自己得去查帮助文档,而且还得去试,因为没有中文版,英文翻译不 ...
随机推荐
- Reduction operations
Reuction operations Reduction operations A reduction operations on a tensor is an operation that red ...
- Akka源码分析-Persistence Query
Akka Persistence Query是对akka持久化的一个补充,它提供了统一的.异步的流查询接口.今天我们就来研究下这个Persistence Query. 前面我们已经分析过Akka Pe ...
- 题解报告:hdu1994利息计算
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1994 Problem Description 为自行解决学费,chx勤工俭学收入10000元以1年定期 ...
- magento 用程序生成优惠劵码
参考自http://fragmentedthought.com/fragments/programatically-creating-sales-rule-coupon-code 上面的代码只能生成C ...
- VS2010下安装Opencv 分类: Opencv 2014-11-02 13:51 778人阅读 评论(0) 收藏
Opencv作为一种跨平台计算机视觉库,在图像处理领域得到广泛的应用,下面介绍如何在VS2010中安装配置Opencv 一.下载Opencv 下载网址:http://sourceforge.net/p ...
- Statistics gathering and SQL Tuning Advisor
1. https://www.pythian.com/blog/statistics-gathering-and-sql-tuning-advisor/ Our monitoring software ...
- MySQL-时间(time、date、datetime、timestamp和year)
情景进入 情境进入: 今天调试某查询页面,偶尔发现一个问题,刚刚插入的数据,没有正常排序显示,经过后台调试sql,发现一个问题??? 经过上面红色对比,不知道你发现问题没,Order by 只是多一个 ...
- 5 Transforms 转移 笔记
5 Transforms 转移 笔记 Transforms Unfortunately, no one can be told what the Matrix is. You have to ...
- 12. binary search Trees
12. binary search Trees The search tree data structure supports many dynamic-set operations,inclu ...
- MySQL——sql注入
https://blog.csdn.net/lin_tuer/article/details/54809330 https://github.com/mysqljs/mysql#escaping-qu ...