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

发布于 2024-12-16 20:02:11 字数 4391 浏览 0 评论 0原文

这是我的 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

我的大问题是我想显示作者和作者。书的标题,所以我将代码修改为

"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 标签,并且我想要作者姓名和作者姓名。书名要换行。

有人能帮我解决上述问题吗?

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

输出应该是这样的:

Erik T. Ray

Learning XML

This is my XML file

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

and this is my Java code:

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());
           }
}
}

the output will be:

    Erik T. Ray

My big problem is I would like to display the author & the title of the book, so I modified the code from

"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));

to be like this:

 "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));

and I get an error:

 Unexpected token "$" beyond end of query

but, if I alter the code like this (adding HTML tag and curly bracket{} ):

   "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 can get the output:

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

The problem is, I don't want to have the HTML tag in my output and I want the author name & the title of the book to be in a new line..

Can anyone help me with this problems mentioned above?

How do I display multiple elements in a new line without HTML in the output?

The output should be like this:

Erik T. Ray

Learning XML

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

远昼 2024-12-23 20:02:11

您可以尝试使用 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(),'
',$book/title/text())";
             System.out.println(tester.query(queryString));

You could try using 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(),'
',$book/title/text())";
             System.out.println(tester.query(queryString));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文