好容易能运行的XQuery示例(Java+Saxon)

 

None.gif package  testUnit;
None.gif
None.gif
import  java.io.IOException;
None.gif
// import java.util.Iterator;
None.gif
// import java.util.List;
None.gif
import  java.util.Properties;
None.gif
None.gif
import  javax.xml.parsers. * ;
None.gif
import  javax.xml.transform. * ;
None.gif
import  javax.xml.transform.dom.DOMSource;
None.gif
import  javax.xml.transform.stream.StreamResult;
None.gif
None.gif
import  org.w3c.dom. * ;
None.gif
import  org.xml.sax.SAXException;
None.gif
None.gif
import  net.sf.saxon.dom. * ;
None.gif
// import net.sf.saxon.om.NodeInfo;
None.gif
import  net.sf.saxon.query. * ;
None.gif
import  net.sf.saxon.trans.XPathException;
None.gif
import  net.sf.saxon. * ;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public   class  XMLTest  dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public static void main(String[] args) dot.gif{
InBlock.gif        query();
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//*
InBlock.gif         * Configuration c = new Configuration(); StaticQueryContext qp = new
InBlock.gif         * StaticQueryContext(c); XQueryExpression xe = qp.compileQuery("");
InBlock.gif         * DynamicQueryContext dqc = new DynamicQueryContext(c);
InBlock.gif         * dqc.setContextNode(new DocumentWrapper(tidyDOM, url, c)); List result =
InBlock.gif         * xe.evaluate(dqc);
ExpandedSubBlockEnd.gif         
*/

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    @SuppressWarnings(
"deprecation")
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public static void query() dot.gif{
InBlock.gif        String query 
= "<ul>\n"
InBlock.gif                
+ "{\n"
InBlock.gif                
+ "for $b in //bib/book\n"
InBlock.gif                
+ "where $b/publisher = \"Addison-Wesley\" and $b/@year > 1992 "
InBlock.gif                
+ "return\n" + "<li>{ data($b/title) }</li>\n" + "}\n"
InBlock.gif                
+ "</ul>";
InBlock.gif        Document doc 
= input("test.xml");
InBlock.gif        Configuration c 
= new Configuration();
InBlock.gif        StaticQueryContext qp 
= new StaticQueryContext(c);
InBlock.gif        XQueryExpression xe;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
try dot.gif{
InBlock.gif            xe 
= qp.compileQuery(query);
InBlock.gif            DynamicQueryContext dqc 
= new DynamicQueryContext(c);
InBlock.gif            dqc.setContextNode(
new DocumentWrapper(doc, null, c));
InBlock.gif            
InBlock.gif            
final Properties props = new Properties();
InBlock.gif            props.setProperty(OutputKeys.METHOD, 
"xml");
InBlock.gif            props.setProperty(OutputKeys.INDENT, 
"yes");
InBlock.gif            xe.run(dqc, 
new StreamResult(System.out), props);
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (XPathException e) dot.gif{
InBlock.gif            
// TODO Auto-generated catch block
InBlock.gif
            e.printStackTrace();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public static Document input(String filename) dot.gif{
InBlock.gif        DocumentBuilderFactory factory 
= DocumentBuilderFactory.newInstance();
InBlock.gif        DocumentBuilder builder;
InBlock.gif
InBlock.gif        Document doc 
= null;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
try dot.gif{
InBlock.gif            builder 
= factory.newDocumentBuilder();
InBlock.gif            doc 
= builder.parse(filename);
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (ParserConfigurationException e) dot.gif{
InBlock.gif            
// TODO Auto-generated catch block
InBlock.gif
            e.printStackTrace();
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (SAXException e1) dot.gif{
InBlock.gif            
// TODO Auto-generated catch block
InBlock.gif
            e1.printStackTrace();
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (IOException e1) dot.gif{
InBlock.gif            
// TODO Auto-generated catch block
InBlock.gif
            e1.printStackTrace();
ExpandedSubBlockEnd.gif        }

InBlock.gif        doc.normalize();
InBlock.gif        
return doc;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public static void output(Document _doc) dot.gif{
InBlock.gif        TransformerFactory tFactory 
= TransformerFactory.newInstance();
InBlock.gif        Transformer transformer;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
try dot.gif{
InBlock.gif            transformer 
= tFactory.newTransformer();
InBlock.gif            Properties properties 
= transformer.getOutputProperties();
InBlock.gif            properties.setProperty(OutputKeys.INDENT, 
"yes");
InBlock.gif            properties.setProperty(OutputKeys.ENCODING, 
"GB2312");
InBlock.gif            properties.setProperty(OutputKeys.METHOD, 
"xml");
InBlock.gif            properties.setProperty(OutputKeys.VERSION, 
"1.0");
InBlock.gif            transformer.setOutputProperties(properties);
InBlock.gif            DOMSource source 
= new DOMSource(_doc);
InBlock.gif            StreamResult result 
= new StreamResult(System.out);
InBlock.gif            transformer.transform(source, result);
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (TransformerConfigurationException e) dot.gif{
InBlock.gif            
// TODO Auto-generated catch block
InBlock.gif
            e.printStackTrace();
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (TransformerException e) dot.gif{
InBlock.gif            
// TODO Auto-generated catch block
InBlock.gif
            e.printStackTrace();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

test.xml内容:
None.gif < bib >
None.gif
< book  year ="1994" >
None.gif
< title > TCP/IP Illustrated </ title >
None.gif
< author >< last > Stevens </ last >< first > W. </ first ></ author >
None.gif
< publisher > Addison-Wesley </ publisher >
None.gif
< price > 65.95 </ price >
None.gif
</ book >
None.gif
< book  year ="1992" >
None.gif
< title > Advanced Programming in the Unix environment </ title >
None.gif
< author >< last > Stevens </ last >< first > W. </ first ></ author >
None.gif
< publisher > Addison-Wesley </ publisher >
None.gif
< price > 65.95 </ price >
None.gif
</ book >
None.gif
< book  year ="2000" >
None.gif
< title > Data on the Web </ title >
None.gif
< author >< last > Abiteboul </ last >< first > Serge </ first ></ author >
None.gif
< author >< last > Buneman </ last >< first > Peter </ first ></ author >
None.gif
< author >< last > Suciu </ last >< first > Dan </ first ></ author >
None.gif
</ book >
None.gif
</ bib >

输出:
None.gif <? xml version="1.0" encoding="UTF-8" ?>
None.gif
< ul >
None.gif   
< li > TCP/IP Illustrated </ li >
None.gif
</ ul >


程序来源于 authorship.gif 成功运行第一个XQuery表达式
修改了一处:1992后面加了一个空格

转载于:https://www.cnblogs.com/anf/archive/2006/08/05/468735.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值