Search code examples
apioracle11goracle-apexapex-codeoracle-apex-5.1

How can I extract only the error message from xml result


How can I extract the value from the label VertexApplicationException and User login failed. from below xml result?

Inside my package, i am using 'make request api' call to get tax value calculated from vertex, sometimes i am getting error as response for my api call, now i need to store the response in table but only i need the error message ex: VertexApplicationException and User login failed. from the belowgiven xml response tag

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <nsf:Fault xmlns:nsf="http://schemas.xmlsoap.org/soap/envelope/">
         <faultcode>nsf:Client</faultcode>
         <faultstring>User login failed.</faultstring>
         <detail>
            <ns:VertexException xmlns:ns="urn:vertexinc:oseries:exception:1:0">
               <ns:exceptionType>VertexApplicationException</ns:exceptionType>
               <ns:rootCause>User login failed.</ns:rootCause>
            </ns:VertexException>
         </detail>`enter code here`
      </nsf:Fault>
   </S:Body>
</S:Envelope>

Solution

  • You can try below query:

    select a.*  from XMLTABLE (  
    
      xmlnamespaces( 'http://schemas.xmlsoap.org/soap/envelope/'   as "S",   
                     'http://schemas.xmlsoap.org/soap/envelope/' as "nsf",
                     'urn:vertexinc:oseries:exception:1:0' as "ns"),  
      '  
      /S:Envelope/S:Body/nsf:Fault/detail/ns:VertexException
      '  
    
      PASSING  
    
      XMLTYPE(  
      '  
      <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
       <S:Body>
          <nsf:Fault xmlns:nsf="http://schemas.xmlsoap.org/soap/envelope/">
             <faultcode>nsf:Client</faultcode>
             <faultstring>User login failed.</faultstring>
             <detail>
                <ns:VertexException xmlns:ns="urn:vertexinc:oseries:exception:1:0">
                   <ns:exceptionType>VertexApplicationException</ns:exceptionType>
                   <ns:rootCause>User login failed.</ns:rootCause>
                </ns:VertexException>
             </detail>`enter code here`
          </nsf:Fault>
       </S:Body>
    </S:Envelope>  
      ')  
    
      columns  
        ExceptionType varchar2(40) path 'ns:exceptionType'  
      , RootCause varchar2(40) path 'ns:rootCause'  
    ) AS A  
    ;  
    

    Check DEMO Here

    Output

    +----------------------------+--------------------+
    | EXCEPTIONTYPE              | ROOTCAUSE          |
    +----------------------------+--------------------+
    | VertexApplicationException | User login failed. |
    +----------------------------+--------------------+