如何使用PL / SQL从XML文件中提取数据

时间:2015-11-04 10:35:59

标签: xml plsql

如何使用PL / SQL从此XML中提取数据?

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<!DOCTYPE BPS SYSTEM "bpml.dtd">
<BPS Created="2012-04-24 11:40:41">
<Machine SerialNumber="" Site="" SoftwareRelease="MAP_248204031" VersionInfo="" Name="" Type="BPS200">
  <ParameterSection Number="6" StartTime="2012-04-24 11:23:01" EndTime="1970-01-01 00:00:00">
    <HeadercardUnit HeaderCardID="1706539" DepositID="01706539" StartTime="2012-04-24 11:39:57" MilliSec="0" EndTime="2012-04-24 11:40:40" Rejects="NO">
      <Counter Currency="HRK" DenomID="22550" Value="200" Quality="Fit" Output="Stacked" Number="192"></Counter>
      <Counter Currency="HRK" DenomID="22550" Value="200" Quality="Unfit" Output="Stacked" Number="7"></Counter>
    </HeadercardUnit>
  </ParameterSection>
</Machine>
</BPS>

XML在DB中保存为CLOB。我需要从XML中提取以下值:

  • 货币
  • DenomID
  • 质量
  • 输出
  • ...

2 个答案:

答案 0 :(得分:0)

您需要能够在PL / SQL中解析该XML。有内置的软件包可以做到这一点。解析XML后,可以使用extract函数从中获取数据。

DECLARE
  x XMLType := XMLType(
    '<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
     <!DOCTYPE BPS SYSTEM "bpml.dtd">
     <BPS Created="2012-04-24 11:40:41">
         <Machine SerialNumber="" Site="" SoftwareRelease="MAP_248204031" VersionInfo="" Name="" Type="BPS200">
             <ParameterSection Number="6" StartTime="2012-04-24 11:23:01" EndTime="1970-01-01 00:00:00">
                 <HeadercardUnit HeaderCardID="1706539" DepositID="01706539" StartTime="2012-04-24 11:39:57" MilliSec="0" EndTime="2012-04-24 11:40:40" Rejects="NO">
                     <Counter Currency="HRK" DenomID="22550" Value="200" Quality="Fit" Output="Stacked" Number="192"></Counter>
                     <Counter Currency="HRK" DenomID="22550" Value="200" Quality="Unfit" Output="Stacked" Number="7"></Counter>
                 </HeadercardUnit>
             </ParameterSection>
         </Machine>
     </BPS>');
BEGIN
  FOR r IN (
    SELECT ExtractValue(Value(p),'/@Currency') as currency
          ,ExtractValue(Value(p),'/@DenomID') as denomid
          ,ExtractValue(Value(p),'/@Value') as val
    FROM   TABLE(XMLSequence(Extract(x,'/BPS/Machine/ParameterSection/HeaderCardUnit/Counter'))) p
    ) LOOP
    -- you can use values for r.currency, r.denomid, r.val
    NULL;
  END LOOP;
END;

答案 1 :(得分:0)

我没有&#34; bpml.dtd&#34;文件。我删除了第二行。

See more information here

DECLARE
  x XMLTYPE := XMLTYPE(
    '<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
     <BPS Created="2012-04-24 11:40:41">
         <Machine SerialNumber="" Site="" SoftwareRelease="MAP_248204031" VersionInfo="" Name="" Type="BPS200">
             <ParameterSection Number="6" StartTime="2012-04-24 11:23:01" EndTime="1970-01-01 00:00:00">
                 <HeadercardUnit HeaderCardID="1706539" DepositID="01706539" StartTime="2012-04-24 11:39:57" MilliSec="0" EndTime="2012-04-24 11:40:40" Rejects="NO">
                     <Counter Currency="HRK" DenomID="22550" Value="200" Quality="Fit" Output="Stacked" Number="192"></Counter>
                     <Counter Currency="HRK" DenomID="22550" Value="200" Quality="Unfit" Output="Stacked" Number="7"></Counter>
                 </HeadercardUnit>
             </ParameterSection>
         </Machine>
     </BPS>');
BEGIN
  FOR y IN (
    SELECT Value(p).extract('/Counter/@Currency').getStringVal() as currency,
          Value(p).extract('/Counter/@DenomID').getNumberVal() as denomid,
          Value(p).extract('/Counter/@Value').getNumberVal() as val,
          Value(p).extract('/Counter/@Quality').getStringVal() as qual,
          Value(p).extract('/Counter/@Output').getStringVal() as output,
           Value(p).extract('/Counter/@Number').getNumberVal() as numb
    FROM   TABLE(XMLSequence(Extract(x,'/BPS/Machine/ParameterSection/HeadercardUnit/Counter'))) p
    ) LOOP

    DBMS_OUTPUT.put_line(y.currency||','||y.denomid||','|| y.val||','||y.qual||','||y.output||','|| y.numb );

    NULL;
  END LOOP;
END;