使用XPath查询oracle数据库时,如何返回值列表而不是字符串?

时间:2015-03-03 17:33:59

标签: java sql xml oracle xpath

我正在使用XPath来查询我正在查询的字段的oracle数据库:

<!-- language: lang-xml -->
<film>
    <title>Godfather, The</title>
    <year>1972</year>
    <directors>
        <director>Francis Ford Coppola</director>
    </directors>
    <genres>
        <genre>Crime</genre>
        <genre>Drama</genre>
    </genres>
    <plot>Son of a mafia boss takes over when his father is critically wounded in a mob hit.</plot>
    <cast>
        <performer>
            <actor>Marlon Brando</actor>
            <role>Don Vito Corleone</role>
        </performer>
        <performer>
            <actor>Al Pacino</actor>
            <role>Michael Corleone</role>
        </performer>
        <performer>
            <actor>Diane Keaton</actor>
            <role>Kay Adams Corleone</role>
        </performer>
        <performer>
            <actor>Robert Duvall</actor>
            <role>Tom Hagen</role>
        </performer>
        <performer>
            <actor>James Caan</actor>
            <role>Sonny Corleone</role>
        </performer>
    </cast>
</film>

我想让所有出演电影的演员归还教父。在我的代码看起来像:

result = stmt.executeQuery("SELECT a.FILM.extract('/film[title=\"Godfather, The\"]/cast/performer/actor/text()') "
                + "FROM ASS2_Film a "
                + "WHERE a.film.existsNode('/film[title=\"Godfather, The\"]')=1");

System.out.println("\nActor");            

while (result.next()) {   
    System.out.println(result.getString(1)+"\n");
}

我的代码返回的那一刻:

Actor
Marlon BrandoAl PacinoDiane KeatonRobert DuvallJames Caan

我希望它在哪里返回:

Actor
Marlon Brando
Al Pacino
Diane Keaton
Robert Duvall
James Caan

感谢您的帮助

1 个答案:

答案 0 :(得分:3)

EXTRACT(和EXTRACTVALUE)是不推荐使用的函数。您应该使用XMLTABLE代替:

with sample_data as (select xmltype('<film>
    <title>Godfather, The</title>
    <year>1972</year>
    <directors>
        <director>Francis Ford Coppola</director>
    </directors>
    <genres>
        <genre>Crime</genre>
        <genre>Drama</genre>
    </genres>
    <plot>Son of a mafia boss takes over when his father is critically wounded in a mob hit.</plot>
    <cast>
        <performer>
            <actor>Marlon Brando</actor>
            <role>Don Vito Corleone</role>
        </performer>
        <performer>
            <actor>Al Pacino</actor>
            <role>Michael Corleone</role>
        </performer>
        <performer>
            <actor>Diane Keaton</actor>
            <role>Kay Adams Corleone</role>
        </performer>
        <performer>
            <actor>Robert Duvall</actor>
            <role>Tom Hagen</role>
        </performer>
        <performer>
            <actor>James Caan</actor>
            <role>Sonny Corleone</role>
        </performer>
    </cast>
</film>') x from dual)
select x.*
from   sample_data sd,
       xmltable('/film[title="Godfather, The"]/cast/performer' passing sd.x
                columns actor varchar2(50) path '//actor',
                        role varchar2(50) path '//role') x;

ACTOR                                              ROLE                                              
-------------------------------------------------- --------------------------------------------------
Marlon Brando                                      Don Vito Corleone                                 
Al Pacino                                          Michael Corleone                                  
Diane Keaton                                       Kay Adams Corleone                                
Robert Duvall                                      Tom Hagen                                         
James Caan                                         Sonny Corleone  

(我已将角色列添加到其他信息中;如果您不需要查看,则只需从XMLTABLE部分的列列表中删除该列。)