转换表格中的Xml内容

时间:2019-06-01 02:45:09

标签: xml oracle oracle-sqldeveloper

我有一个表,该表的字段包含XML内容,我将在一个表中转换该XML内容,其中每个标签都是一列。 有人知道怎么做吗?

2 个答案:

答案 0 :(得分:1)

您需要事先了解标签,但是您可以为此使用XMLTABLE,例如,如果表table_with_employee_xml包含列xml_col,如下所示

<employees>
  <employee>
    <empno>7369</empno>
    <ename>SMITH</ename>
    <job>CLERK</job>
    <hiredate>17-DEC-1980</hiredate>
  </employee>
  <employee>
    <empno>7499</empno>
    <ename>ALLEN</ename>
    <job>SALESMAN</job>
    <hiredate>20-FEB-1981</hiredate>
  </employee>
  ...
  ...

select x.*
from   table_with_employee_xml e,
       xmltable('/employees/employee'
         passing e.xml_col
         columns 
           empno     varchar2(4)  path 'empno',
           ename     varchar2(10) path 'ename',
           job       varchar2(9)  path 'job',
           hiredate  varchar2(11) path 'hiredate'
         ) x;

答案 1 :(得分:0)

您可以使用以下代码:

CREATE TABLE EDMU_XML (
  id        NUMBER,
  xml_data  XMLTYPE
);


insert into EDMU_XML values (1,'<employees>
  <employee>
    <empno>1</empno>
    <ename>EDMU1</ename>
    <job>SALESMAN</job>\
  </employee>
  <employee>
    <empno>2</empno>
    <ename>EDMU2</ename>
    <job>MANAGER</job>
  </employee>
  <employee>
    <empno>3</empno>
    <ename>EDMU3</ename>
    <job>MANAGER</job>
  </employee>
  <employee>
    <empno>4</empno>
    <ename>EDMU4</ename>
    <job>ANALYST</job>
  </employee>
</employees>');


CREATE TABLE EDMU_NORMAL AS
SELECT xt.*
FROM   EDMU_XML x,
       XMLTABLE('/employees/employee'
         PASSING x.xml_data
         COLUMNS 
           empno     VARCHAR2(4)  PATH 'empno',
           ename     VARCHAR2(10) PATH 'ename',
           job       VARCHAR2(9)  PATH 'job'
         ) xt;

Demo