我在将 xml 数据插入到 oracle 表中时遇到问题,这是我的 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 行数据(如第二行数据)。
更新:这是我当前的代码
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;