[英]Querying XML files using BaseX
I am using BaseX native XML database to query XML files. 我正在使用BaseX原生XML数据库来查询XML文件。 I am using the BaseXClient.java file provided in the BaseX documentation.
我正在使用BaseX文档中提供的BaseXClient.java文件。 I am starting the basex server and connecting to server using BaseXClient.java.
我正在启动basex服务器并使用BaseXClient.java连接到服务器。
// create session
final BaseXClient session = new BaseXClient("localhost", 1984, "admin", "admin");
String query = "doc('xmlfiles/juicers.xml')//image";
// version 1: perform command and print returned string
System.out.println(session.execute(query));
Now the juicers.xml file has xmlns
information. 现在juicers.xml文件包含
xmlns
信息。
<?xml version="1.0"?>
<juicers
xmlns="http://www.juicers.org"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.juicers.org
juicers.xsd">
<juicer>
<name>OJ Home Juicer</name>
<image>images\mighty_oj.gif</image>
<description>There's just no substitute for a properly squeezed
orange in the morning. So delicate and refreshing. The finest hotels
use mechanical juicers of this type for their most discriminating
guests. This is the largest selling juicer of its kind. It's a
beautiful little all-metal piece in baked enamel and polished chrome;
it even won the Frankfurt Fair Award for its design. Uses no
electricity and produces no non-recyclable waste as do frozen juices.
</description>
<warranty>lifetime warranty</warranty>
<cost>41.95</cost>
<retailer>http://www.thewhitewhale.com/oj.htm</retailer>
</juicer>
</juicers>
If I don't give the xmlns
in XML instance file (juicers.xml), it returns me correct results. 如果我不在XML实例文件(juicers.xml)中提供
xmlns
,它会返回正确的结果。 But if xmlns
is included in XML instance file, the following exception is thrown. 但是,如果XML实例文件中包含
xmlns
,则会引发以下异常。
java.io.IOException: Stopped at line 1, column 3:
Expecting command.
at org.basex.api.BaseXClient.execute(BaseXClient.java:73)
at org.basex.api.BaseXClient.execute(BaseXClient.java:84)
at org.basex.api.Example.main(Example.java:31)
How do I query XML instances files with xmlns
? 如何使用
xmlns
查询XML实例文件? Is there a way out? 有出路吗? Is there any other way to run
xquery
from Java? 有没有其他方法可以从Java运行
xquery
?
In addition to Chrstian's answer, you have to either declare a default element namespace or use the namespace each time you address an element (you probably want to do this if you'd have multiple namespaces in your document). 除了Chrstian的答案之外,您还必须声明默认元素命名空间,或者每次处理元素时都使用命名空间(如果文档中有多个命名空间,则可能需要这样做)。
The default element namespace enables you to write your query like you did above: 使用默认元素命名空间可以像上面一样编写查询:
declare default element namespace "http://www.juicers.org";
doc('xmlfiles/juicers.xml')//image
If you do not want to use juicers as default element namespace, declare it as namespace and reference it at element-level: 如果您不想将juicers用作默认元素名称空间,请将其声明为名称空间并在元素级别引用它:
declare namespace juicers="http://www.juicers.org";
doc('xmlfiles/juicers.xml')//juicers:image
You can set the namespace identifier juicers
arbitrarily. 您可以设置命名空间标识
juicers
随意。
You need to prefix your query with the XQUERY
command: 您需要在查询前添加
XQUERY
命令:
System.out.println(session.execute("XQUERY " + query));
Another option is to create a "Query" instance and call query.execute()
after that: 另一种选择是创建一个“Query”实例并在此之后调用
query.execute()
:
BaseXClient.Query query = session.query(query);
System.out.println(query.execute())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.