爱猫的狗

拥抱变化

导航

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

 

package testUnit;

import java.io.IOException;
//import java.util.Iterator;
//import java.util.List;
import java.util.Properties;

import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

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

import net.sf.saxon.dom.*;
//import net.sf.saxon.om.NodeInfo;
import net.sf.saxon.query.*;
import net.sf.saxon.trans.XPathException;
import net.sf.saxon.*;

public class XMLTest {
    
public static void main(String[] args) {
        query();
        
/*
         * Configuration c = new Configuration(); StaticQueryContext qp = new
         * StaticQueryContext(c); XQueryExpression xe = qp.compileQuery("");
         * DynamicQueryContext dqc = new DynamicQueryContext(c);
         * dqc.setContextNode(new DocumentWrapper(tidyDOM, url, c)); List result =
         * xe.evaluate(dqc);
         
*/

    }


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

    }


    
public static Document input(String filename) {
        DocumentBuilderFactory factory 
= DocumentBuilderFactory.newInstance();
        DocumentBuilder builder;

        Document doc 
= null;
        
try {
            builder 
= factory.newDocumentBuilder();
            doc 
= builder.parse(filename);
        }
 catch (ParserConfigurationException e) {
            
// TODO Auto-generated catch block
            e.printStackTrace();
        }
 catch (SAXException e1) {
            
// TODO Auto-generated catch block
            e1.printStackTrace();
        }
 catch (IOException e1) {
            
// TODO Auto-generated catch block
            e1.printStackTrace();
        }

        doc.normalize();
        
return doc;
    }


    
public static void output(Document _doc) {
        TransformerFactory tFactory 
= TransformerFactory.newInstance();
        Transformer transformer;
        
try {
            transformer 
= tFactory.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();
        }

    }

}

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

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


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

posted on 2006-08-05 16:59  anf  阅读(1555)  评论(0编辑  收藏  举报