Search code examples
xmlsoaporacle12c

Oracle 12c, Store SOAP response into variable or insert to table


I'm new to PL/SQL and working with SOAP ws. I managed to get SOAP response XML, and I am using XMLTable to extract data from it, but I get strange format of the data. Here is the select I am having trouble with:

select item
            from XMLTable(
  XMLNamespaces (
    default 'urn:DHCPProv',
    'http://schemas.xmlsoap.org/soap/envelope/' as "soap",
    'http://schemas.xmlsoap.org/soap/encoding/' as "soapenc"
  ),
  '/soap:Envelope/soap:Body/getDhcpForPortResponse/soapenc:Array/item/item'
  passing XMLType('<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" 
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
            <getDhcpForPortResponse
                xmlns="urn:DHCPProv">
                <soapenc:Array
                    soapenc:arrayType="soapenc:Array[2]"
                    xsi:type="soapenc:Array">
                    <item
                        soapenc:arrayType="xsd:string[5]"
                        xsi:type="soapenc:Array">
                        <item
                            xsi:type="xsd:string">
                            qbtp8482tv
                            </item>
                        <item
                            xsi:type="xsd:string">
                            111.11.111.111
                            </item>
                        <item
                            xsi:type="xsd:string">
                            bc644ba2501c
                            </item>
                        <item
                            xsi:type="xsd:string">
                            MF5601T_AMD-NDF
                            </item>
                        <item
                            xsi:type="xsd:string"/>
                        </item>
                    <item
                        soapenc:arrayType="xsd:string[5]"
                        xsi:type="soapenc:Array">
                        <item
                            xsi:type="xsd:string">
                            qbtp8482tv
                            </item>
                        <item
                            xsi:type="xsd:string">
                            222.22.222.222
                            </item>
                        <item
                            xsi:type="xsd:string">
                            704fb8f3e4e1
                            </item>
                        <item
                            xsi:type="xsd:string">
                            MF5601T_AMD-NDF
                            </item>
                        <item
                            xsi:type="xsd:string"/>
                        </item>
                    </soapenc:Array>
                </getDhcpForPortResponse>
 </soap:Body>
 </soap:Envelope>')
  columns item varchar2(4000) path '.'
);

I was wondering if there is a way to store this format into a variable or insert it into a table somehow? Thanks!


Solution

  • Your XML has newlines and whitespace within the node values. If you want to remove those you can do:

    select rtrim(ltrim(item, chr(32)||chr(10)), chr(10)||chr(32)) as item
    

    which gives 10 rows:

    ITEM
    --------------
    qbtp8482tv
    111.11.111.111
    bc644ba2501c
    MF5601T_AMD-NDF
    (null)
    qbtp8482tv
    222.22.222.222
    704fb8f3e4e1
    MF5601T_AMD-NDF
    (null)
    

    You can exclude the nulls with a filter:

    where rtrim(ltrim(item, chr(32)||chr(10)), chr(10)||chr(32)) is not null;
    

    which gives 8 rows:

    ITEM
    --------------
    qbtp8482tv
    111.11.111.111
    bc644ba2501c
    MF5601T_AMD-NDF
    qbtp8482tv
    222.22.222.222
    704fb8f3e4e1
    MF5601T_AMD-NDF
    

    If you want to insert those into a table then just put insert into your_table (your_column) in from of the select ...

    db<>fiddle