Oracle XML:跳过不存在节点 [英] Oracle XML : Skip Not exist Node
本文介绍了Oracle XML:跳过不存在节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在将xml数据插入到oracle表中时遇到问题,这是我的xml:
I have a problem proceed xml data to insert in oracle table, here is my xml :
<begin>
<entry>
<lastname>gordon</lastname>
<NumberList>
<number>100</number>
<codelist>
<code>213</code>
<codelist>
<NumberList>
<address>
<addresslist>Jl. jalan pelan-pelan ke Bekasi, Indonesia</addresslist>
</address>
</entry>
<entry>
<lastname>mark</lastname>
<address>
<addresslist>Jl. jalan cepet-cepet ke Jakarta, Indonesia</addresslist>
</address>
</entry>
</begin>
我期望的结果是:
LastName | Number | code | address
gordon | 100 | 213 |Jl. jalan pelan-pelan ke Bekasi, Indonesia
mark | Null | Null |Jl. jalan cepet-cepet ke Jakarta, Indonesia
访问不存在的节点时,不会出现1行数据(如第二行数据).
When accessing not exist node, 1 row data will not appear ( like second row data).
更新: 这是我当前的代码
Update : here's my current code
DECLARE
X XMLTYPE := XMLTYPE('
<begin>
<entry>
<lastname>gordon</lastname>
<NumberList>
<number>100</number>
<codelist>
<code>213</code>
</codelist>
</NumberList>
<address>
<addresslist>Jl. jalan pelan-pelan ke Bekasi, Indonesia</addresslist>
</address>
</entry>
<entry>
<lastname>mark</lastname>
<address>
<addresslist>Jl. jalan cepet-cepet ke Jakarta, Indonesia</addresslist>
</address>
</entry>
</begin>');
BEGIN
FOR R IN (SELECT EXTRACTVALUE(VALUE(P), '/entry/lastname/text()') AS lastname,
EXTRACTVALUE(VALUE(P), '/entry/address/addresslist/text()') AS address,
EXTRACT(VALUE(P), '/entry/NumberList') AS NumberList
FROM TABLE(XMLSEQUENCE(EXTRACT(X, '//begin/entry'))) P)
LOOP
FOR R1 IN (SELECT EXTRACTVALUE(VALUE(T1), 'NumberList/number/text()') AS numbers,
EXTRACTVALUE(VALUE(T1), 'NumberList/codelist/code/text()') AS code
FROM TABLE(XMLSEQUENCE(EXTRACT(R.NumberList, '/NumberList'))) T1)
LOOP
DBMS_OUTPUT.PUT_LINE(r.lastname||' | '||R1.numbers||' | '||R1.code||' | '||r.address);
END LOOP;
END LOOP;
END;
推荐答案
以下是目标表:
CREATE TABLE my_test
(
LastName varchar2(20),
phone NUMBER,
code varchar2(20),
address VARCHAR2(100)
);
填充它的代码:
SET serveroutput ON
DECLARE
l_xml xmltype;
l_val VARCHAR2(1000) := '<begin>
<entry>
<lastname>gordon</lastname>
<numberlist>
<number>100</number>
<codelist>
<code>213</code>
</codelist>
</numberlist>
<address>
<addresslist>jl. jalan pelan-pelan ke bekasi, indonesia</addresslist>
</address>
</entry>
<entry>
<lastname>mark</lastname>
<address>
<addresslist>jl. jalan cepet-cepet ke jakarta, indonesia</addresslist>
</address>
</entry>
</begin>';
l_lastname varchar2(50);
l_phone number;
l_code number;
l_address varchar2(200);
BEGIN
l_xml := xmltype(l_val);
FOR x IN
(SELECT VALUE(p) col_val
FROM TABLE(XMLSEQUENCE(EXTRACT(l_xml, '/begin/entry'))) p
)
loop
IF x.col_val.existsnode('/entry/lastname/text()') > 0 THEN
l_lastname := x.col_val.extract('/entry/lastname/text()').getstringval();
END IF;
if x.col_val.existsnode('/entry/numberlist/number/text()') > 0 then
l_phone := x.col_val.extract('/entry/numberlist/number/text()').getstringval();
end if;
if x.col_val.existsnode('/entry/numberlist/codelist/code/text()') > 0 then
l_code := x.col_val.extract('/entry/numberlist/codelist/code/text()').getstringval();
end if;
IF x.col_val.existsnode('/entry/address/addresslist/text()') > 0 THEN
l_address := x.col_val.extract('/entry/address/addresslist/text()').getstringval();
end if;
INSERT INTO my_test
(
lastname,
phone,
code,
address
)
VALUES
(
l_lastname,
l_phone,
l_code,
l_address
);
l_lastname := null;
l_phone := null;
l_code := null;
l_address := null;
end loop;
commit;
end;
这篇关于Oracle XML:跳过不存在节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文