如何仅从 xml 结果中提取错误消息

How can I extract only the error message from xml result

如何从 xml 结果下方的标签 VertexApplicationExceptionUser login failed. 中提取值?

在我的包中,我正在使用 'make request api' 调用来获取从顶点计算的税值,有时我收到错误作为对我的 api 调用的响应,现在我需要将响应存储在table 但只有我需要错误消息,例如:VertexApplicationException 和用户登录失败。来自下面给出的 xml 响应标签

<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>

这会让你非常接近,但是 XML 特定的查询也可以完成这项工作

regexp_substr(your_string, '<ns:rootCause>([A-Z]*)(.*)')

您可以尝试以下查询:

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

输出

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