提取Oracle Clob的节点值

时间:2017-11-08 19:17:52

标签: xml oracle clob

我有一个CLOB列,其中包含XML样式的数据,但它肯定似乎没有父子节点关系;它看起来像这样:

<servicePointExternalId>8629391888</servicePointExternalId><externalSPType>E-M-COM</externalSPType><faType>M-STARTS</faType><requesterUserId>E05920</requesterUserId><replyToExternalSystem>D1YS</replyToExternalSystem><externalReferenceId>47676141503102</externalReferenceId><retryDetails><numberOfRetries>0</numberOfRetries><isToDoEntrySuppressed>false</isToDoEntrySuppressed><retryDateTime>2017-04-28-18.16.53</retryDateTime><currentErrorState>VALERROR</currentErrorState></retryDetails><contactDetails><customerName>Hofstader, Leonard</customerName><contactName>Hofstader, Leonard</contactName><accountId>5669202300</accountId><personId>9538791588</personId></contactDetails><saList><eventType>D1ST</eventType><saId>5668577181</saId></saList>

我试图从这个字段中提取requesteruserID值(应该是E05920),我收到此错误消息:

ORA-31011: XML parsing failed
ORA-19213: error occurred in XML processing at lines 1
LPX-00245: extra data after end of document
ORA-06512: at "SYS.XMLTYPE", line 272
ORA-06512: at line 1
31011. 00000 -  "XML parsing failed"
*Cause:    XML parser returned an error while trying to parse the document.
*Action:   Check if the document to be parsed is valid.

我做错了什么?

1 个答案:

答案 0 :(得分:0)

您的示例不是完整的XML文档 - 它没有父节点,只有相邻的子节点。

如果要将其视为XML,则需要将其包装在虚拟根节点中,例如:如果您的CLOB位于名为clob_col的列中,您可以执行以下操作:

xmltype(to_clob('<root>') || clob_col || '</root>')

要获取所需的特定节点值,您可以执行以下操作:

select xmlquery(
  '/root/requesterUserId/text()'
  passing xmltype(to_clob('<root>') || clob_col || '</root>')
  returning content) as result
from your_table;

RESULT                                                                          
--------------------------------------------------------------------------------
E05920

或者您添加了表格和列名称作为评论:

select xmlquery(
  '/root/requesterUserId/text()'
  passing xmltype(to_clob('<root>') || A.BO_DATA_AREA || '</root>')
  returning content) as result
from CISADM.D1_ACTIVITY A

或只是

select xmlquery(
  '/root/requesterUserId/text()'
  passing xmltype('<root>' || A.BO_DATA_AREA || '</root>')
  returning content) as result
from CISADM.D1_ACTIVITY A

您可以转换返回另一种数据类型的XML片段:

select cast(
  xmlquery( '/root/requesterUserId/text()'
    passing xmltype(to_clob('<root>') || A.BO_DATA_AREA|| '</root>')
    returning content)
  as varchar2(8)) as requesterUserId
from CISADM.D1_ACTIVITY A;

或作为替代方案,您可以使用XMLTable代替XMLQuery

select x.requesterUserId
from CISADM.D1_ACTIVITY A
cross join xmltable(
  '/root'
  passing xmltype('<root>' || A.BO_DATA_AREA || '</root>')
  columns requesterUserId varchar2(8) path 'requesterUserId'
) x;

其中任何一个都给出一个纯字符串值:

REQUESTE
--------
E05920

Read more about XML processing