2012-03-31 25 views
3

私はBaseXネイティブXMLデータベースを使用してXMLファイルを照会しています。 BaseXのドキュメントで提供されているBaseXClient.javaファイルを使用しています。私はBaseXサーバーを起動し、BaseXClient.javaを使用してサーバーに接続しています。BaseXを使用してXMLファイルを照会

// 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&apos;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&apos;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を実行する他の方法はありますか?

答えて

3

要素の名前を指定するたびに、デフォルトの要素名前空間を宣言するか、名前空間を使用する必要があります(文書内に複数の名前空間がある場合は、これを行うことをお勧めします)。

デフォルトの要素の名前空間を使用すると、上記のやったように、あなたのクエリを書くことができます:

declare default element namespace "http://www.juicers.org"; 
doc('xmlfiles/juicers.xml')//image 

あなたは、デフォルトの要素の名前空間としてジューサーを使用し、名前空間として宣言し、要素レベルでそれを参照したくない場合:

declare namespace juicers="http://www.juicers.org"; 
doc('xmlfiles/juicers.xml')//juicers:image 

あなたは、任意の名前空間識別子juicersを設定することができます。

1

あなたはXQUERYコマンドを使用して、クエリの前に付ける必要があります。

System.out.println(session.execute("XQUERY " + query)); 

別のオプションは、「クエリ」のインスタンスを作成し、その後query.execute()を呼び出すことです:Chrstianの答えに加えて

BaseXClient.Query query = session.query(query); 
System.out.println(query.execute()) 
+0

ありがとうございました。しかし、私は、XMLインスタンスファイルに定義されているXML名前空間がない場合、またはxmlインスタンスファイルで指定されたスキーマファイル場所がない場合にのみクエリを実行できます。私は上記の存在、結果は返されません。この動作の特定の理由はありますか? –

関連する問題