java xquery xpath的使用问题小白

想要用java Xquery来把一个xml文档中的信息 提取出来的,但是XQuery转换后变成了xml string文件,所以想要用XPath来把这个string数据进行转换并且排版,但是我写的程序不知道哪里有误,拜托大神们帮我看看

import java.io.StringReader;
import java.util.Properties;

import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import com.ddtek.xquery3.XQConnection;
import com.ddtek.xquery3.XQException;
import com.ddtek.xquery3.XQExpression;
import com.ddtek.xquery3.XQItemType;
import com.ddtek.xquery3.XQSequence;
import com.ddtek.xquery3.xqj.DDXQDataSource;


public class XQueryBookstore {

    // Filename for XML document to query
      private String filename;

      // Data Source for querying
      private DDXQDataSource dataSource;
      // Connection for querying
      private XQConnection conn;

      public XQueryBookstore(String filename) {
        this.filename = filename;
      }

      public void init() throws XQException {
        dataSource = new DDXQDataSource();
        conn = dataSource.getConnection();
      }

      public String query(String queryString) throws XQException {
        XQExpression expression = conn.createExpression();
        expression.bindString(new QName("docName"), filename,
          conn.createAtomicType(XQItemType.XQBASETYPE_STRING));
        XQSequence results = expression.executeQuery(queryString);


        return results.getSequenceAsString(new Properties());
      }
      public static void main(String[] args) {
        if (args.length != 1) {
          System.err.println("Usage: java ibm.dw.xqj.XQueryBookstore [XML filename]");
          System.exit(-1);
        }
        String jgg;

        try {
          String xmlFilename = args[0];
          XQueryBookstore tester = new XQueryBookstore(xmlFilename);
          tester.init();

          final String sep = System.getProperty("line.separator");
          String queryString = 
            "declare variable $docName as xs:string external;" + sep +
            "      for $book in doc($docName)/bookstore/books/book " +
            "    where $book/price > 30.00 " +
            "   return " +
            "<book><title>{$book/title/text()}</title>" + 
            " <year>{$book/year/text()}</year></book>";

          jgg = tester.query(queryString);

          System.out.println(tester.query(queryString));

          InputSource is = new InputSource(new StringReader(jgg)); 
          Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is);

          XPath xpath = XPathFactory.newInstance().newXPath();

            String expression = "/book";

            NodeList cols = (NodeList) xpath.compile(expression).evaluate(document, XPathConstants.NODESET);

            System.out.println("제목\t\t연도");

            for (int idx = 0; idx < cols.getLength(); idx++) {

                expression = "/book";

                String title = xpath.compile(expression).evaluate(document);

                System.out.print(title + "\t");




                expression = "/book";

                String year = xpath.compile(expression).evaluate(document);

                System.out.print(year + "\t");


            }


        }
            catch(Exception e){

            }

        }




      }

这个是xml文件

<?xml version="1.0" encoding="utf-8" ?>
<bookstore>

 <books>
 <book ssn="001">
   <title>Everyday Korean</title> 
   <author>Luis Xin</author> 
   <year>2017</year> 
   <price>50.00</price> 
 </book>

 <book ssn="002">
   <title>Harry Potter</title> 
   <author>J K. Rowling</author> 
   <year>2005</year> 
   <price>29.99</price> 
 </book>

 <book ssn="003">
   <title>Learning XML</title> 
   <author>Erik T. Ray</author> 
   <year>2003</year> 
   <price>39.95</price> 
 </book>

 <book ssn="004">
   <title>Learning JAVA</title> 
   <author>Erik     </author> 
   <year>2005</year> 
   <price>40.00</price> 
 </book>

 <book ssn="005">
   <title>Korean Food</title> 
   <author>Ancher Su</author> 
   <year>2015</year> 
   <price>40.00</price> 
 </book>

 </books>
</bookstore>

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问