zoukankan      html  css  js  c++  java
  • Oracle中对XMLType的简单操作(extract、extractvalue...)

    Oracle中对XMLType的简单操作(extract、extractvalue...)

     

     1、下面先创建一个名未test.xml的配置文件。

    <?xml version="1.0" encoding="UTF-8" ?>
    <collection xmlns="">
      <record>
        <leader>-----nam0-22-----^^^450-</leader>
        <datafield tag="200" ind1="1" ind2=" ">
          <subfield code="a">抗震救灾</subfield>
           <subfield code="f">奥运会</subfield>
        </datafield>
        <datafield tag="209" ind1=" " ind2=" ">
          <subfield code="a">经济学</subfield>
           <subfield code="b">计算机</subfield>
           <subfield code="c">10001</subfield>
           <subfield code="d">2005-07-09</subfield>
        </datafield>
        <datafield tag="610" ind1="0" ind2=" ">
           <subfield code="a">计算机</subfield>
           <subfield code="a">笔记本</subfield>
        </datafield>
      </record>
    </collection>

    2、在Oracle中创建一个存放该XML文件的表:

    复制代码
    create or replace table xmlexample(
    ID varchar2(10),
    name varchar2(20),
    data xmltype
    )
    insert into xmlexample(id,name,data)
    values(sys_guid(),'my document',
            xmltype
            (
             bfilename(filepath,filename),
             nls_charset_id('AL32UTF8')
            )
    )
    复制代码

    其中filepath为文件路径,filename为文件名。

    3、extractvalue()函数的使用
      Oracle提供对XML文件的检索功能(extractvalue),extractvalue只能返回一个节点的一个值,具体操作方法如下:

    select id,name,
          extractvalue(x.data,'/collection/record/leader') as A
    from xmlexample x;

    ID         NAME                 A
    ---------- -------------------- ----------------------------------
    1          my document          -----nam0-22-----^^^450-

    如果该节点有两个值,则系统提示错误。

    4、extract()函数的使用
    如果想查询所有subfield的值就要用到extract(),它可以返回一个节点下的所有值。操作如下

    select id,name,
             extract(x.data,'/collection/record/datafield/subfield') as A
    from xmlexample x;

    ID         NAME                 A
    ---------- -------------------- ---------------------------------------------------------
    1          my document          <subfield xmlns="" code="a">抗震救灾</subfield><subfield xmlns="" code="f">何观.....

    可以看到它返回的是XML格式的。如果我们想只返回它值就要是用两个函数了(见5)。

     5、table和XMLSequence
    操作如下:

    select extractValue(value(i),'/subfield') xx
    from xmlexample x,
    table(XMLSequence(extract(x.data,'/collection/record/datafield/subfield'))) i;

    XX
    --------------------------------------------------------------------------------
    抗震救灾
    何观华
    经济学
    计算机
    10001
    2005-07-09
    计算几  哈哈

    7 rows selected。

    6、检索出特定的节点的特定值
    有时候我们在实际操作的时候并不是检索出所有值,而是根据条件查询出我们所需要的信息。如果我们想检索出
        <datafield tag="209" ind1=" " ind2=" ">
          <subfield code="a">经济学</subfield>
    中的值-经济学

    操作如下:

    select id,name,
            extractvalue(x.data,'/collection/record/datafield[@tag="209"]/subfield[@code="a"]') as A
    from xmlexample x;

    ID         NAME                 A
    ---------- -------------------- ---------------------------
    1          my document          经济学

    7、总结

    Oracle对与XMLType的操作有很多种,还要靠大家自己去发现。数据库对XML的检索就是吧XML的节点当作一个列来检索,而不同的是表里装的是二维的数据,而XML中可以装N维。还有就是,表中列不存在就会提示无效标识符,如果节点不存在,则检索出NULL,不会报错。所以,对与XML文件的操作通常是通过视图来完成。

  • 相关阅读:
    年末反思
    Flink运行时架构
    Phoenix 启动报错:Error: ERROR 726 (43M10): Inconsistent namespace mapping properties. Cannot initiate connection as SYSTEM:CATALOG is found but client does not have phoenix.schema.
    Clickhouse学习
    Flink简单认识
    IDEA无法pull代码到本地,Can't Update No tracked branch configured for branch master or the branch doesn't exist.
    第1章 计算机系统漫游
    简单的 Shell 脚本入门教程
    开源≠免费 常见开源协议介绍
    MySQL 视图
  • 原文地址:https://www.cnblogs.com/LuZhiYou1994/p/6795235.html
Copyright © 2011-2022 走看看