用saxon框架对xml数据进行过滤
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代码
<?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源码
Java代码
public class Test2 {
public static void main(String[] args) {
try{
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
//从文档中加载xml内容
class.getResourceAsStream("/flight/flight_data.xml");
Document document = builder.parse(in);
//去掉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表达式
new StringBuffer();
" for $s in /flight/row where 1=1 ");
" and contains(upper-case($s/@flightno), 'CA') ");
" and contains(upper-case($s/@city_namecn), '海') ");
" and upper-case($s/@airline_code)='CA' ");
" and $s/@flight_date='20130202' ");
" and $s/@flight_time>='2300' ");
" and $s/@flight_time<='2300' ");
" and $s/@status_code='fly' ");
" return $s ");
new Configuration();
//静态查询上下文
new StaticQueryContext(configuration);
XQueryExpression expression = context.compileQuery(sb.toString());
//动态查询上下文
new DynamicQueryContext(configuration);
new DocumentWrapper(document, null, configuration));
new Properties();
"xml");
"yes");
"GBK");
"1.0");
//根据xQuery表达式解析xml文件,返回符合条件的数据,存储到writer对象
new StringWriter();
new StreamResult(writer), props);
System.out.println(writer.toString());
catch(Exception ex){
ex.printStackTrace();
}
}
}
3、输出结果
Xml代码
<?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"/>
作者:少帅