我正在使用BaseX原生XML数据库来查询XML文件。我使用的是BaseX文档中提供的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));现在,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>如果我没有在XML实例文件(juicers.xml)中给出xmlns,它会返回正确的结果。但是如果xmlns包含在XML实例文件中,就会抛出以下异常。
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)如何使用xmlns查询XML实例文件?有没有出路?有没有其他方法可以在Java中运行xquery?
发布于 2012-04-01 19:54:40
除了Chrstian的回答之外,您还必须声明一个默认的元素名称空间,或者在每次寻址一个元素时使用该名称空间(如果文档中有多个名称空间,则可能需要这样做)。
默认的元素名称空间使您可以像上面一样编写查询:
declare default element namespace "http://www.juicers.org";
doc('xmlfiles/juicers.xml')//image如果您不想使用juicers作为默认的元素命名空间,请将其声明为命名空间并在元素级别引用它:
declare namespace juicers="http://www.juicers.org";
doc('xmlfiles/juicers.xml')//juicers:image您可以任意设置命名空间标识符juicers。
发布于 2012-04-01 04:26:49
您需要在查询前添加XQUERY命令:
System.out.println(session.execute("XQUERY " + query));另一种选择是创建一个"Query“实例,然后调用query.execute():
BaseXClient.Query query = session.query(query);
System.out.println(query.execute())https://stackoverflow.com/questions/9959159
复制相似问题