yatongnuo4627 2017-08-21 04:26 采纳率: 0%
浏览 1091

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条回答 默认 最新

  • devmiao 2017-08-22 16:36
    关注
    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况