這是我的XML文件如何在Java中使用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
我最大的問題是我想顯示作者&這本書的標題,所以我修改了代碼
"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
感謝DevNull的解決方案! – user1050754
不過,如果我評論的 //「其中$書/年= 2003」 爲什麼輸出變成了這個樣子: 日常意大利 嘉妲·狄羅倫提斯哈利波特 JK。羅琳學習XML 埃裏克ŧ Ray 再次感謝! – user1050754
我的意思是結果不在一個新的線了..想問一下,這是' '?謝謝@DevNull – user1050754