第一个在java中使用xquery的例子

第一个在java中使用xquery的例子

一、使用eclipse新建一个java project
二、把saxon的jar包放入classpath(我用的jar包是saxon9ee.jar)
三、新建package,然后新建一个xml文件(cd_catalog.xml),其内容如下:
<?xml version="1.0" encoding="ISO-8859-1"?>
    <CATALOG>
         <CD>
              <TITLE>Empire Burlesque</TITLE>
              <ARTIST>Bob Dylan</ARTIST>
              <COUNTRY>USA</COUNTRY>
               <COMPANY>Columbia</COMPANY>
              <PRICE>10.90</PRICE>
              <YEAR>1985</YEAR>
         </CD>
         <CD>
              <TITLE>Hide your heart</TITLE>
              <ARTIST>Bonnie Tyler</ARTIST>
              <COUNTRY>UK</COUNTRY>
              <COMPANY>CBS Records</COMPANY>
              <PRICE>9.90</PRICE>
              <YEAR>1988</YEAR>
         </CD>
         <CD>
              <TITLE>Greatest Hits</TITLE>
              <ARTIST>Dolly Parton</ARTIST>
              <COUNTRY>USA</COUNTRY>
              <COMPANY>RCA</COMPANY>
              <PRICE>9.90</PRICE>
              <YEAR>1982</YEAR>
         </CD>
         <CD>
              <TITLE>Still got the blues</TITLE>
              <ARTIST>Gary Moore</ARTIST>
              <COUNTRY>UK</COUNTRY>
              <COMPANY>Virgin records</COMPANY>
              <PRICE>10.20</PRICE>
              <YEAR>1990</YEAR>
         </CD>
         <CD>
              <TITLE>Eros</TITLE>
              <ARTIST>Eros Ramazzotti</ARTIST>
              <COUNTRY>EU</COUNTRY>
              <COMPANY>BMG</COMPANY>
              <PRICE>9.90</PRICE>
              <YEAR>1997</YEAR>
         </CD>
         <CD>
              <TITLE>One night only</TITLE>
              <ARTIST>Bee Gees</ARTIST>
              <COUNTRY>UK</COUNTRY>
              <COMPANY>Polydor</COMPANY>
              <PRICE>10.90</PRICE>
              <YEAR>1998</YEAR>
         </CD>
         <CD>
              <TITLE>Sylvias Mother</TITLE>
              <ARTIST>Dr.Hook</ARTIST>
              <COUNTRY>UK</COUNTRY>
              <COMPANY>CBS</COMPANY>
              <PRICE>8.10</PRICE>
              <YEAR>1973</YEAR>
         </CD>
         <CD>
                <TITLE>Maggie May</TITLE>
              <ARTIST>Rod Stewart</ARTIST>
              <COUNTRY>UK</COUNTRY>
              <COMPANY>Pickwick</COMPANY>
              <PRICE>8.50</PRICE>
              <YEAR>1990</YEAR>
         </CD>
         <CD>
               <TITLE>Romanza</TITLE>
              <ARTIST>Andrea Bocelli</ARTIST>
              <COUNTRY>EU</COUNTRY>
              <COMPANY>Polydor</COMPANY>
              <PRICE>10.80</PRICE>
              <YEAR>1996</YEAR>
       </CD>
         <CD>
              <TITLE>When a man loves a woman</TITLE>
              <ARTIST>Percy Sledge</ARTIST>
              <COUNTRY>USA</COUNTRY>
              <COMPANY>Atlantic</COMPANY>
              <PRICE>8.70</PRICE>
              <YEAR>1987</YEAR>
         </CD>
   </CATALOG>
        
四、新建一个java class(XqueryTest.java)

import java.io.IOException;
import java.util.Properties;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import net.sf.saxon.Configuration;
import net.sf.saxon.dom.*;
import net.sf.saxon.query.DynamicQueryContext;
import net.sf.saxon.query.StaticQueryContext;
import net.sf.saxon.query.XQueryExpression;
import net.sf.saxon.trans.XPathException;

public class XqueryTest {
 /**
  * 执行查询
  */
 public static void select(){
  //文件
  String fileString = "src/study/xquery/cd_catalog.xml";
  //查询语句
  String query = " for $s in //CD/TITLE "
    + " return $s";
  //生产文档对象
  Document document = getDocument(fileString);
  Configuration configuration = new Configuration();
  StaticQueryContext context = new StaticQueryContext(configuration, false);
  //查询表达式对象
  XQueryExpression expression = null;
  try {
   expression = context.compileQuery(query);
   DynamicQueryContext context2 = new DynamicQueryContext(configuration);
   context2.setContextItem(new DocumentWrapper(document,null,configuration));
   
   final Properties props = new Properties();
            props.setProperty(OutputKeys.METHOD, "xml");
            props.setProperty(OutputKeys.INDENT, "yes");
            //执行查询,并输出查询结果
            expression.run(context2, new StreamResult(System.out), props);
  } catch (XPathException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
 }
 /**
  * 生产文档对象
  *
  * @param xml   文件名
  * @return
  */
 public static Document getDocument(String xml){
  DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
  DocumentBuilder builder;
  Document document = null;
  try {
   builder = builderFactory.newDocumentBuilder();
   document = builder.parse(xml);
  } catch (ParserConfigurationException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (SAXException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  document.normalize();
  return document;
 }
 /**
  * 输入生成的文档内容
  *
  * @param doc
  */
 public static void output(Document doc){
  TransformerFactory factory = TransformerFactory.newInstance();
  Transformer transformer = null;
  try {
   transformer = factory.newTransformer();
   Properties properties = transformer.getOutputProperties();
   properties.setProperty(OutputKeys.INDENT, "yes");
            properties.setProperty(OutputKeys.ENCODING, "GB2312");
            properties.setProperty(OutputKeys.METHOD, "xml");
            properties.setProperty(OutputKeys.VERSION, "1.0");
            transformer.setOutputProperties(properties);
           
            DOMSource source = new DOMSource(doc);
            StreamResult result = new StreamResult(System.out);
            transformer.transform(source, result);
   
  } catch (TransformerConfigurationException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (TransformerException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 public static void main(String[] args) {
  select();
 }
}


五、运行结果
<?xml version="1.0" encoding="UTF-8"?>
<TITLE>Empire Burlesque</TITLE>
<TITLE>Hide your heart</TITLE>
<TITLE>Greatest Hits</TITLE>
<TITLE>Still got the blues</TITLE>
<TITLE>Eros</TITLE>
<TITLE>One night only</TITLE>
<TITLE>Sylvias Mother</TITLE>
<TITLE>Maggie May</TITLE>
<TITLE>Romanza</TITLE>
<TITLE>When a man loves a woman</TITLE>

你可能感兴趣的:(第一个在java中使用xquery的例子)