【WebService】Stax的基本操作基于游标
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bookstore>
<book category="COOKING">
<title lang="en">Giada De Laurentiis</title>
<author>Lee</author>
<year>2005</year>
<price>30.00</price>
</book> <book category="WEB">
<title lang="en">JQuery</title>
<author>Lee</author>
<author>Sun</author>
<author>James</author>
<year>2015</year>
<price>30.00</price>
</book>
</bookstore>
package com.slp.stax; import org.junit.Test; import javax.xml.stream.*;
import javax.xml.stream.events.XMLEvent;
import java.io.IOException;
import java.io.InputStream; /**
* Created by sanglp on 2017/2/26.
*/
public class TestStax { /**
* Bookstore book title
Giada De Laurentiis
/title author
Lee
/author year
2005
/year price
30.00
/price /book book title
JQuery
/title author
Lee
/author author
Sun
/author author
James
/author year
2015
/year price
30.00
/price /book /bookstore */
@Test
public void test01(){
XMLInputFactory factory = XMLInputFactory.newInstance();
InputStream is = null;
try {
is = TestStax.class.getClassLoader().getResourceAsStream("books.xml");
XMLStreamReader reader = factory.createXMLStreamReader(is);
while (reader.hasNext()){
int type = reader.next();
//判断节点类型
if(type== XMLStreamConstants.START_ELEMENT){
System.out.println(reader.getName());
}else if(type==XMLStreamConstants.CHARACTERS){
System.out.println(reader.getText().trim());
}else if(type==XMLStreamConstants.END_ELEMENT){
System.out.println("/"+reader.getName());
}
// System.out.println(reader.next());
}
} catch (XMLStreamException e) {
e.printStackTrace();
}finally {
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} @Test
public void test02(){
XMLInputFactory factory = XMLInputFactory.newInstance();
InputStream is = null;
try {
is = TestStax.class.getClassLoader().getResourceAsStream("books.xml");
XMLStreamReader reader = factory.createXMLStreamReader(is);
while (reader.hasNext()){
int type = reader.next();
if(type== XMLStreamConstants.START_ELEMENT){
String name = reader.getName().toString();
if(name.equals("book")){
System.out.println(reader.getAttributeName(0)+":"+reader.getAttributeValue(0));
}
}
}
} catch (XMLStreamException e) {
e.printStackTrace();
}finally {
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} /**
* Giada De Laurentiis:30.00
JQuery:30.00
*/
@Test
public void test03(){
XMLInputFactory factory = XMLInputFactory.newInstance();
InputStream is = null;
try {
is = TestStax.class.getClassLoader().getResourceAsStream("books.xml");
XMLStreamReader reader = factory.createXMLStreamReader(is);
while (reader.hasNext()){
int type = reader.next();
if(type== XMLStreamConstants.START_ELEMENT){
String name = reader.getName().toString();
if(name.equals("title")){
System.out.print(reader.getElementText()+":");
}
if(name.equals("price")){
System.out.print(reader.getElementText()+"\n");
}
}
}
} catch (XMLStreamException e) {
e.printStackTrace();
}finally {
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} /**
* Giada De Laurentiis:30.00
* JQuery:30.00
*/
@Test
public void test04(){
XMLInputFactory factory = XMLInputFactory.newInstance();
InputStream is = null;
try {
is = TestStax.class.getClassLoader().getResourceAsStream("books.xml");
//基于迭代模型的操作方式
XMLEventReader reader = factory.createXMLEventReader(is);
while (reader.hasNext()){
//通过XMLEvent来获取是否是某种节点类型
XMLEvent event = reader.nextEvent();
if(event.isStartElement()){
//通过event.asxxx()转换节点
String name = event.asStartElement().getName().toString();
if(name.equals("title")){
System.out.print(reader.getElementText()+":");
}
if(name.equals("price")){
System.out.print(reader.getElementText()+"\n");
}
}
}
} catch (XMLStreamException e) {
e.printStackTrace();
}finally {
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} /**
* Giada De Laurentiis:30.00
* JQuery:30.00
*/
@Test
public void test05(){
XMLInputFactory factory = XMLInputFactory.newInstance();
InputStream is = null;
try {
is = TestStax.class.getClassLoader().getResourceAsStream("books.xml");
//基于Filter的过滤方式 可以有效的过滤不用进行操作的节点,效率会高一些
XMLEventReader reader = factory.createFilteredReader(factory.createXMLEventReader(is),new EventFilter(){
public boolean accept(XMLEvent event){
//放true表示会显示,返回false表示不显示
if(event.isStartElement())
return true;
return false;
}
});
int num =0;
while (reader.hasNext()){
//通过XMLEvent来获取是否是某种节点类型
XMLEvent event = reader.nextEvent();
if(event.isStartElement()){
//通过event.asxxx()转换节点
String name = event.asStartElement().getName().toString();
if(name.equals("title")){
System.out.print(reader.getElementText()+":");
}
if(name.equals("price")){
System.out.print(reader.getElementText()+"\n");
}
}
num++;
}
} catch (XMLStreamException e) {
e.printStackTrace();
}finally {
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
【WebService】Stax的基本操作基于游标的更多相关文章
- 基于游标的定位DELETE/UPDATE语句
如果游标是可更新的(也就是说,在定义游标语句中不包括Read Only 参数),就可以用游标从游标数据的源表中DELETE/UPDATE行,即DELETE/UPDATE基于游标指针的当前位置的操作: ...
- 主题:实战WebService II: SOAP篇(基于php)
概述(SOAP和XML-PRC比较) 在Web服务发展的初期,XML格式化消息的第一个主要用途是,应用于XML-RPC协议,其中RPC代表远程过程调用.在XML远程过程调用 (XML-RPC)中,客户 ...
- axis1.4开发webservice客户端(快速入门)-基于jdk1.4
写在前面: 对于客户端,服务端开发好了以后,客户端只需要调用就可以了.这里我们讲的是,根据服务的访问地址,来生成客户端所需要用到的代码(听说有几种调用方式,但是用到最常见的就是stub方式,貌似我说的 ...
- java对mongoDB的基本操作 ,游标使用
package com.mongodb.text; import java.net.UnknownHostException; import java.util.List; import org.bs ...
- 代码片段,lucene基本操作(基于lucene4.10.2)
1.最基本的创建索引: @Test public void testIndex(){ try { Directory directory = FSDirectory.open(new File(LUC ...
- 浅谈c语言的线性表的基本操作———基于严蔚敏的数据结构(c语言版)
主要讲的是线性表的创建,插入及删除: 0. 线性表的建立,对于这类操作主要是利用了结构体的性质,对于定义的线性表的特性主要有三点:首先 Typedef struct { ElemType *ele ...
- Sql Server系列:游标
1. 游标简介 游标是一种处理数据的方法,主要用于存储过程.触发器和Transact-SQL脚本中.SELECT语句返回的是一个结果集,游标能够从包含多条数据记录的结果集中每次提取一条记录. 游标的特 ...
- SQL Server基础之游标
查询语句可能返回多条记录,如果数据量非常大,需要使用游标来逐条读取查询结果集中的记录.应用程序可以根据需要滚动或浏览其中的数据.本篇介绍游标的概念.分类.以及基本操作等内容. 一:认识游标 游标是 ...
- [推荐]ORACLE PL/SQL编程之四:把游标说透(不怕做不到,只怕想不到)
原文:[推荐]ORACLE PL/SQL编程之四:把游标说透(不怕做不到,只怕想不到) [推荐]ORACLE PL/SQL编程之四: 把游标说透(不怕做不到,只怕想不到) 继上两篇:ORACLE PL ...
随机推荐
- c#利用反射Assembly 对类和成员属性进行操作
protected static void test() { //获取程序集 Assembly assembly = System.Reflection.Assembly.GetExecutingAs ...
- Linux 含有ext 分区的镜像无法再Windows上拷贝的解决办法
首先,参考http://www.cnblogs.com/chenfulin5/p/6649801.html 通过上面这个链接,做一个镜像出来. // 1. 制作一个镜像文件,里面包含200M左右的空间 ...
- Iconfont在移动端遇到问题的探讨
Iconfont越来越得到前端的关注,特别是阿里那个iconfont库的推出和不断完善,再加上连IE6都能兼容,的确是个好东西. 既然那么火,我们公司移动项目也计划加入这个iconfont,于是我就针 ...
- css 图片文字对齐
默认情况,是图片置顶对齐,文字置底对齐,所以通常图片高,文字低,不能水平居中对齐 解决办法:在css中设置图片的vertical-align属性, <img src="" s ...
- Cracking the coding interview--Q2.5
题目 原文: Given a circular linked list, implement an algorithm which returns node at the beginning of t ...
- LIBSVM使用方法及参数设置 主要参考了一些博客以及自己使用经验。
主要参考了一些博客以及自己使用经验.收集来觉得比较有用的. LIBSVM 数据格式需要---------------------- 决策属性 条件属性a 条件属性b ... 2 1:7 ...
- Hadoop书签
1)http://www.cnblogs.com/forfuture1978/archive/2010/03/14/1685351.html 2)http://www.cnblogs.com/sund ...
- linux -- Ubuntuserver图形界面下安装、配置lampp、phpmyadmin
PHP开发和服务器运行环境首选LAMP组合,即Linux+Apache+Mysql+Php/Perl/Python,能最优化服务器性能.如何在本地电脑Ubuntu 中安装和配置LAMP环境搭建?Ubu ...
- Hasen的linux设备驱动开发学习之旅--时钟
/** * Author:hasen * 參考 :<linux设备驱动开发具体解释> * 简单介绍:android小菜鸟的linux * 设备驱动开发学习之旅 * 主题:时钟 * Date ...
- oracle当前月添加一列显示前几个月的累计值
create table test_leiji(rpt_month_id number(8), current_month NUMBER(12,2)); ...