PL / SQL - XML clob错误

时间:2017-11-17 21:22:28

标签: xml oracle plsql clob xmltype

我正在尝试测试一个以xml CLOB作为参数的PL / SQL过程

PROCEDURE example_proc(ex_xml CLOB) IS
BEGIN

/* Find the second < (skip over declaration) */
ln_position := instr(pc_shipment_xml, '<', 1, 2);
/* Find the space after the first tag */
ln_position := instr(pc_shipment_xml, ' ', ln_position, 1);
/* Set the first part of the XML clob */
lc_shipment_xml := substr(pc_shipment_xml, 1, ln_position - 1);
/* Skip the namespace */
ln_position     := instr(pc_shipment_xml, '>', ln_position, 1);
lc_shipment_xml := lc_shipment_xml || substr(pc_shipment_xml, ln_position);

/* Set the XML type */
lx_shipment_xml := xmltype(lc_shipment_xml); --RIGHT HERE IS WHERE IT ERRORS OUT

这是我传入的xml(作为ex_xml clob参数):

    xml := 
'<Shipment>
  <OrderNumber>00000</OrderNumber>
   <ShipmentLines>
    <ShipmentLine>
      <OrderDetailId>000000</OrderDetailId>
      <LineNumber>0</LineNumber>
      <ItemId>00000</ItemId>
      <UnitId>0000000</UnitId>
      <BaseUom>X</BaseUom>
      <Quantity1>000</Quantity1>
    </ShipmentLine>
  </ShipmentLines>
</Shipment>';

并且在最后一行出错:

ORA-19202: Error occurred in XML processing
LPX-002255: end-element tag "ShipmentLines" does not match start-element tag "Shipment"

请原谅我的无知,但这是我第一次在PL / SQ中使用XML,我错过了格式化的东西吗?据我所知,我正确地打开和关闭我的标签。任何输入都是值得赞赏的。

2 个答案:

答案 0 :(得分:0)

将代码作为

运行
declare
  pc_shipment_xml CLOB := '<Shipment>
  <OrderNumber>00000</OrderNumber>
   <ShipmentLines>
    <ShipmentLine>
      <OrderDetailId>000000</OrderDetailId>
      <LineNumber>0</LineNumber>
      <ItemId>00000</ItemId>
      <UnitId>0000000</UnitId>
      <BaseUom>X</BaseUom>
      <Quantity1>000</Quantity1>
    </ShipmentLine>
  </ShipmentLines>
</Shipment>';
  lc_shipment_xml varchar2(2000);
  ln_position integer;
BEGIN
  /* Find the second < (skip over declaration) */
  ln_position := instr(pc_shipment_xml, '<', 1, 2);
  /* Find the space after the first tag */
  ln_position := instr(pc_shipment_xml, ' ', ln_position, 1);
  /* Set the first part of the XML clob */
  lc_shipment_xml := substr(pc_shipment_xml, 1, ln_position - 1);
  /* Skip the namespace */
  ln_position     := instr(pc_shipment_xml, '>', ln_position, 1) + 1;
  lc_shipment_xml := lc_shipment_xml || substr(pc_shipment_xml, ln_position);
  dbms_output.put_line(lc_shipment_xml);
end;
/

给出以下输出:

<Shipment>
  <OrderNumber>00000</OrderNumber>

    <ShipmentLine>
      <OrderDetailId>000000</OrderDetailId>
      <LineNumber>0</LineNumber>
      <ItemId>00000</ItemId>
      <UnitId>0000000</UnitId>
      <BaseUom>X</BaseUom>
      <Quantity1>000</Quantity1>
    </ShipmentLine>
  </ShipmentLines>
</Shipment>

所以看起来你正在剥离开始的ShipmentLines标签,而不是结束标签

如果你知道要删除的标记的名称,可以使用

更简单地删除它们
lc_shipment_xml := regexp_replace(pc_shipment_xml, '</?ShipmentLines>');

或按位置获取标记:

l_tag := regexp_substr(pc_shipment_xml, '<(\w+)>',1,3,'i',1);
lc_shipment_xml := regexp_replace(pc_shipment_xml, '</?' || l_tag || '>');

更好的方法是使用xml功能,如下所示:

declare
  pc_shipment_xml CLOB := '<Shipment>
  <OrderNumber>00000</OrderNumber>
  <ShipmentLines>
    <ShipmentLine>
      <OrderDetailId>000000</OrderDetailId>
      <LineNumber>0</LineNumber>
      <ItemId>00000</ItemId>
      <UnitId>0000000</UnitId>
      <BaseUom>X</BaseUom>
      <Quantity1>000</Quantity1>
    </ShipmentLine>
  </ShipmentLines>
</Shipment>';
  lx_shipment_xml xmltype := xmltype(pc_shipment_xml);
  lx_shipline_xml xmltype := xmltype(pc_shipment_xml);
  lc_shipment_xml varchar2(2000);
BEGIN
  /* Set the XML type */
    select extract(lx_shipment_xml, '/Shipment/ShipmentLines/ShipmentLine') into lx_shipline_xml from dual;
    select deletexml(lx_shipment_xml, '/Shipment/ShipmentLines') into lx_shipment_xml from dual;
    select insertxmlafter(lx_shipment_xml, '/Shipment/OrderNumber', lx_shipline_xml) into lx_shipment_xml from dual;
    select xmlserialize(content lx_shipment_xml) into lc_shipment_xml from dual;
    dbms_output.put_line(lc_shipment_xml);
end;
/

答案 1 :(得分:0)

我解析xml的方式需要将<?xml version="1.0"?>放在clob的开头。解析方法不是我自己的代码,所以改变我的方式并不是一个选择。感谢