如何使用Java中的XQuery从XML文件中显示新行中的多个元素?

时间:2011-11-18 04:03:03

标签: java xml xquery

这是我的XML文件

<bookstore>
    <book category="COOKING">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>
  <book category="CHILDREN">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
  <book category="WEB">
    <title lang="en">Learning XML</title>
    <author>Erik T. Ray</author>
    <year>2003</year>
    <price>39.95</price>
 </book>
</bookstore>

这是我的Java代码:

import javax.xml.namespace.QName;
import java.util.Properties;

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 XQueryTester3 {

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

// Data Source for querying
private DDXQDataSource dataSource;

// Connection for querying
private XQConnection conn;

public XQueryTester3(String filename) {
this.filename = "untitled1.xml";
}

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) {

        try {

             XQueryTester3 tester = new XQueryTester3("untitled1.xml");
             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/book " +
                            "    where $book/year =2003 " +
                            "   return " +
                            "$book/author/text()";
             System.out.println(tester.query(queryString));
           } catch (Exception e) {
             e.printStackTrace(System.err);
             System.err.println(e.getMessage());
           }
}
}

输出将是:

    Erik T. Ray

我最大的问题是我想展示作者&amp;这本书的标题,所以我从

修改了代码
"declare variable $docName as xs:string external;" + sep +
                            "      for $book in doc($docName)/bookstore/book " +
                            "    where $book/year =2003 " +
                            "   return " +
                            "$book/author/text()";
             System.out.println(tester.query(queryString));

是这样的:

 "declare variable $docName as xs:string external;" + sep +
                            "      for $book in doc($docName)/bookstore/book " +
                            "    where $book/year =2003 " +
                            "   return " +
                            "$book/author/text() " +
                            "$book/title/text()";
             System.out.println(tester.query(queryString));

我收到错误:

 Unexpected token "$" beyond end of query

但是,如果我像这样更改代码(添加HTML标记和大括号{}):

   "declare variable $docName as xs:string external;" + sep +
                            "      for $book in doc($docName)/bookstore/book " +
                            "    where $book/year =2003 " +
                            "   return " +
                            "<i>{$book/author/text()} " +
                            "{$book/title/text()}</i>";
             System.out.println(tester.query(queryString));

我可以得到输出:

<i>Erik T. RayLearning XML</i>

问题是,我不希望在输出中包含HTML标记,我想要作者姓名&amp;这本书的标题是新的一行..

任何人都可以帮助我解决上述问题吗?

如何在输出中没有HTML的情况下在新行中显示多个元素?

输出应该是这样的:

Erik T. Ray

Learning XML

2 个答案:

答案 0 :(得分:1)

您可以尝试使用concat()

"declare variable $docName as xs:string external;" + sep +
                            "      for $book in doc($docName)/bookstore/book " +
                            "    where $book/year =2003 " +
                            "   return " +
                            "concat($book/author/text(),'&#xA;',$book/title/text())";
             System.out.println(tester.query(queryString));

答案 1 :(得分:0)

我找到了一个问题的解决方案:我使用的是XPath,而不是XQuery。

幸运的是我遇到了this link

显示了一切。