Oracle
 sql >> база данни >  >> RDS >> Oracle

Oracle XML:Пропускане Не съществува възел

Ето целевата таблица:

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;


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Как да промените приоритета за едновременна програма

  2. Върнете името на краткия месец от дата в Oracle

  3. Документация на MAA за Oracle Cloud

  4. Метод за събиране:Процедура DELETE в базата данни на Oracle

  5. Извикване на pl/sql функция в java?