用saxon框架对xml数据进行过滤 - 程序员的天堂 - ITeye技术网站
Saxon 是一个 XSLT 和XQuery处理器。它是使用 XML 文档和样式表作为输入,然后生成结果文档作为输出的程序,它还包括了一个串行化器,用于将结果树转换成 XML、HTML 或纯文本。
Saxon8以上版本主要组成有:XSLT 2.0处理器、 XPath 2.0处理器、 XQuery 1.0处理器、XML Schema 1.0处理器。
XPath 教程: http://www.w3school.com.cn/xpath/index.asp
XQuery 教程:http://www.w3school.com.cn/xquery/index.asp
以下范例代码在saxonb9-1-0-8j包下测试通过:
1、xml文档内容
- <?xml version="1.0" encoding="UTF-8"?>
- <flight>
- <row flightno="CA3411" airline_code="CA" airline_namecn="中国国际航空公司" airline_nameen="Air China" city_code="SHA" city_namecn="上海虹桥" city_nameen="Shanghai" flight_date="20130202" flight_time="2200" status_code="cancel" status_namecn="取消" status_nameen="Cancel" checkin_counter="M2-3" gate="A118"/>
- <row flightno="CA3411" airline_code="CA" airline_namecn="中国国际航空公司" airline_nameen="Air China" city_code="SHA" city_namecn="上海虹桥" city_nameen="Shanghai" flight_date="20130202" flight_time="2300" status_code="fly" status_namecn="起飞" status_nameen="Fly" checkin_counter="M2-3" gate="A118"/>
- <row flightno="CZ3412" airline_code="CZ" airline_namecn="中国南方航空公司" airline_nameen="South Air" city_code="PEK" city_namecn="北京" city_nameen="Beijing" flight_date="20130203" flight_time="2200" status_code="fly" status_namecn="起飞" status_nameen="Fly" checkin_counter="M1-3" gate="A218"/>
- </flight>
<?xml version="1.0" encoding="UTF-8"?><flight> <row flightno="CA3411" airline_code="CA" airline_namecn="中国国际航空公司" airline_nameen="Air China" city_code="SHA" city_namecn="上海虹桥" city_nameen="Shanghai" flight_date="20130202" flight_time="2200" status_code="cancel" status_namecn="取消" status_nameen="Cancel" checkin_counter="M2-3" gate="A118"/> <row flightno="CA3411" airline_code="CA" airline_namecn="中国国际航空公司" airline_nameen="Air China" city_code="SHA" city_namecn="上海虹桥" city_nameen="Shanghai" flight_date="20130202" flight_time="2300" status_code="fly" status_namecn="起飞" status_nameen="Fly" checkin_counter="M2-3" gate="A118"/> <row flightno="CZ3412" airline_code="CZ" airline_namecn="中国南方航空公司" airline_nameen="South Air" city_code="PEK" city_namecn="北京" city_nameen="Beijing" flight_date="20130203" flight_time="2200" status_code="fly" status_namecn="起飞" status_nameen="Fly" checkin_counter="M1-3" gate="A218"/></flight>
2、java源码
- public class Test2 {
- public static void main(String[] args) {
- try{
- DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
- DocumentBuilder builder = builderFactory.newDocumentBuilder();
- //从文档中加载xml内容
- InputStream in = Class.class.getResourceAsStream("/flight/flight_data.xml");
- Document document = builder.parse(in);
- document.normalize(); //去掉XML文档中空白部分
- //从字符串中加载xml内容
- //StringReader sr = new StringReader("<flight><row flightno=\"CA3411\" airline_code=\"CA\" airline_namecn=\"中国国际航空公司\" airline_nameen=\"Air China\" city_code=\"SHA\" city_namecn=\"上海虹桥\" city_nameen=\"Shanghai\" flight_date=\"20130202\" flight_time=\"2300\" status_code=\"fly\" status_namecn=\"起飞\" status_nameen=\"Fly\" checkin_counter=\"M2-3\" gate=\"A118\"/></flight>");
- //InputSource is = new InputSource(sr);
- //Document document = builder.parse(is);
- //document.normalize(); //去掉XML文档中空白部分
- //xQuery表达式
- StringBuffer sb = new StringBuffer();
- sb.append(" for $s in /flight/row where 1=1 ");
- sb.append(" and contains(upper-case($s/@flightno), 'CA') ");
- sb.append(" and contains(upper-case($s/@city_namecn), '海') ");
- sb.append(" and upper-case($s/@airline_code)='CA' ");
- sb.append(" and $s/@flight_date='20130202' ");
- sb.append(" and $s/@flight_time>='2300' ");
- sb.append(" and $s/@flight_time<='2300' ");
- sb.append(" and $s/@status_code='fly' ");
- sb.append(" return $s ");
- Configuration configuration = new Configuration();
- //静态查询上下文
- StaticQueryContext context = new StaticQueryContext(configuration);
- XQueryExpression expression = context.compileQuery(sb.toString());
- //动态查询上下文
- DynamicQueryContext context2 = new DynamicQueryContext(configuration);
- context2.setContextItem(new DocumentWrapper(document, null, configuration));
- Properties props = new Properties();
- props.setProperty(OutputKeys.METHOD, "xml");
- props.setProperty(OutputKeys.INDENT, "yes");
- props.setProperty(OutputKeys.ENCODING, "GBK");
- props.setProperty(OutputKeys.VERSION, "1.0");
- //根据xQuery表达式解析xml文件,返回符合条件的数据,存储到writer对象
- Writer writer = new StringWriter();
- expression.run(context2, new StreamResult(writer), props);
- System.out.println(writer.toString());
- }catch(Exception ex){
- ex.printStackTrace();
- }
- }
- }
public class Test2 { public static void main(String[] args) { try{ DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = builderFactory.newDocumentBuilder(); //从文档中加载xml内容 InputStream in = Class.class.getResourceAsStream("/flight/flight_data.xml"); Document document = builder.parse(in); document.normalize(); //去掉XML文档中空白部分 //从字符串中加载xml内容 //StringReader sr = new StringReader("<flight><row flightno=\"CA3411\" airline_code=\"CA\" airline_namecn=\"中国国际航空公司\" airline_nameen=\"Air China\" city_code=\"SHA\" city_namecn=\"上海虹桥\" city_nameen=\"Shanghai\" flight_date=\"20130202\" flight_time=\"2300\" status_code=\"fly\" status_namecn=\"起飞\" status_nameen=\"Fly\" checkin_counter=\"M2-3\" gate=\"A118\"/></flight>"); //InputSource is = new InputSource(sr); //Document document = builder.parse(is); //document.normalize(); //去掉XML文档中空白部分 //xQuery表达式 StringBuffer sb = new StringBuffer(); sb.append(" for $s in /flight/row where 1=1 "); sb.append(" and contains(upper-case($s/@flightno), 'CA') "); sb.append(" and contains(upper-case($s/@city_namecn), '海') "); sb.append(" and upper-case($s/@airline_code)='CA' "); sb.append(" and $s/@flight_date='20130202' "); sb.append(" and $s/@flight_time>='2300' "); sb.append(" and $s/@flight_time<='2300' "); sb.append(" and $s/@status_code='fly' "); sb.append(" return $s "); Configuration configuration = new Configuration(); //静态查询上下文 StaticQueryContext context = new StaticQueryContext(configuration); XQueryExpression expression = context.compileQuery(sb.toString()); //动态查询上下文 DynamicQueryContext context2 = new DynamicQueryContext(configuration); context2.setContextItem(new DocumentWrapper(document, null, configuration)); Properties props = new Properties(); props.setProperty(OutputKeys.METHOD, "xml"); props.setProperty(OutputKeys.INDENT, "yes"); props.setProperty(OutputKeys.ENCODING, "GBK"); props.setProperty(OutputKeys.VERSION, "1.0"); //根据xQuery表达式解析xml文件,返回符合条件的数据,存储到writer对象 Writer writer = new StringWriter(); expression.run(context2, new StreamResult(writer), props); System.out.println(writer.toString()); }catch(Exception ex){ ex.printStackTrace(); } }}
3、输出结果
- <?xml version="1.0" encoding="GBK"?>
- <row airline_code="CA" airline_namecn="中国国际航空公司" airline_nameen="Air China"
- checkin_counter="M2-3"
- city_code="SHA"
- city_namecn="上海虹桥"
- city_nameen="Shanghai"
- flight_date="20130202"
- flight_time="2300"
- flightno="CA3411"
- gate="A118"
- status_code="fly"
- status_namecn="起飞"
- status_nameen="Fly"/>
<?xml version="1.0" encoding="GBK"?><row airline_code="CA" airline_namecn="中国国际航空公司" airline_nameen="Air China" checkin_counter="M2-3" city_code="SHA" city_namecn="上海虹桥" city_nameen="Shanghai" flight_date="20130202" flight_time="2300" flightno="CA3411" gate="A118" status_code="fly" status_namecn="起飞" status_nameen="Fly"/>
用saxon框架对xml数据进行过滤 - 程序员的天堂 - ITeye技术网站的更多相关文章
- 总结2015搭建日志,监控,ci,前端路由,数据平台,画的图与界面 - hugo - ITeye技术网站
总结2015搭建日志,监控,ci,前端路由,数据平台,画的图与界面 - hugo - ITeye技术网站 极分享:高质分享+专业互助=没有难做的软件+没有不得已的加班 极分享:高质分享+专业互助=没有 ...
- MyBatis框架的XML数据访问Dao层接口的组合使用
MyBatis 的前生为Apache的开源项目iBatis.其优势在于灵活,几乎可以替代JDBC,同时提供了编程接口.目前MyBatis的数据访问Dao层不需要实现类,也不需要像JDBC那样拼接Hql ...
- 一个必用的javascript框架:underscore.js - wine的思考 - ITeye技术网站
AngularJS+JqueryMobile+PhoneGap 打造APP « Dogeek AngularJS+JqueryMobile+PhoneGap 打造APP
- 苦B程序员的数据验证之路
发生了什么事 在一次苦B程序员和苦C程序员的结对编程中发生的一段对话 代码是这样的: public void deleteAllExtendAclsFromContent(String content ...
- spring 框架的xml文件如何读取properties文件数据
spring 框架的xml文件如何读取properties文件数据 第一步:在spring配置文件中 注意:value可以多配置几个properties文件 <bean id="pro ...
- 记录SSM框架项目迁移SpringBoot框架-----pom.xml的迁移
第一步:迁移pom.xml文件(去除spring相关的依赖) SSM中的pom: <project xmlns="http://maven.apache.org/POM/4.0.0&q ...
- 一个简单xml数据转换为数组的方法
本人用easywechat做微信回复图文,从数据库中拿到的数据直接是xml拼好的数据,但是框架只有自带的获取xml格式的语句,所有需要将xml数据中所需要的数据拿出来用来拼接. 搜了好多资料说的都很麻 ...
- iOS开发——网络篇——JSON和XML,NSJSONSerialization ,NSXMLParser(XML解析器),NSXMLParserDelegate,MJExtension (字典转模型),GDataXML(三方框架解析XML)
一.JSON 1.JSON简介什么是JSONJSON是一种轻量级的数据格式,一般用于数据交互服务器返回给客户端的数据,一般都是JSON格式或者XML格式(文件下载除外) JSON的格式很像OC中的字典 ...
- XML 数据请求与JSON 数据请求
(1)XML 数据请求 使用 AFNetworking 中的 AFHTTPRequestOperation 和 AFXMLParserResponseSerializer,另外结合第三方框架 XMLD ...
随机推荐
- 理解Java String和String Pool
本文转载自: http://blog.sina.com.cn/s/blog_5203f6ce0100tiux.html 要理解 java中String的运作方式,必须明确一点:String是一个非可变 ...
- Hibernate Tools
(声明)本文转自:http://linjia880714.iteye.com/blog/859334 hibernate-tools详细使用教程 使用hibernate-tool的版本是hiberna ...
- sql求日期
2.求以下日期SQL: 昨天 select convert(varchar(10),getdate() - 1,120) 明天 select convert(varchar(10),getdate() ...
- JPA 系列教程3-单向多对一
JPA中的@ManyToOne 主要属性 - name(必需): 设定"many"方所包含的"one"方所对应的持久化类的属性名 - column(可选): 设 ...
- XMEAG-128A1
JTAGUSERID = 0x00 WDWP = 8CLK WDP = 8CLK DVSDON = [ ] BOOTRST = BOOTLDR BODPD = DISABLED RSTDISBL = ...
- AIX创建用户
-bash-3.2# mkuser test 创建用户3004-689 User "test" exists.-bash-3.2# finge ...
- 自定义开关ToggleButton
package com.example.test;import android.os.Bundle;import android.app.Activity;import android.view.Me ...
- 网络获取的XML的Pull解析
<?xml version="1.0" encoding="utf-8" ?> - <students> - <student x ...
- cocos2d-js 显示帧序列图中的一帧
1.flashCC中打开库,在一个元件中右键->Generate Sprite Sheet...设置如下: 2.点Export后得到playerWalk.png和playerWalk.plist ...
- 转:Warning -26490: File name in a multipart submit is missing or empty.解决方法
录制测试上传文件脚本,回放报Warning -26490: File name in a multipart submit is missing or empty. Using an empty fi ...