0

我有 xml,其中嵌入了 CDATA 中的数据。我想提取不同领域的信息。但做不到。

<Item_Response Format="text/xml">   
<![CDATA[ <Item sequence="1" type="item" itemId="999999" 
itemVersion="2012-04-07T13:43:27">
<response><bubbleinput answered="y" input_id="bubbleinput1">
<bubble id="bubble1"/>
</bubbleinput></response></Item> ]]>
</Item_Response> 
4

1 回答 1

1

您可以使用 XPath 提取 CDATA 值。然后对提取的值使用 openxml。

declare @xml xml = '<Item_Response Format="text/xml"><![CDATA[ <Item sequence="1" type="item" itemId="999999" itemVersion="2012-04-07T13:43:27"><response><bubbleinput answered="y" input_id="bubbleinput1"><bubble id="bubble1"/></bubbleinput></response></Item> ]]></Item_Response>'

-- openxml
declare
    @idoc int,
    @qxml xml = cast(@xml.value('(/Item_Response)[1]', 'nvarchar(max)') as xml)

exec sp_xml_preparedocument @idoc output, @qxml

select
    *
from
    openxml(@idoc, '/Item', 0) with (
        sequence int '@sequence',
        bubbleinput nvarchar(1) './response/bubbleinput/@answered'
    ) as XMLData

exec sp_xml_removedocument @idoc
于 2015-07-09T08:19:50.270 回答