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
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办