阅读XML专栏

时间:2013-09-03 06:59:37

标签: sql sql-server xml

我的XML记录为我的SQL数据库字段,我试图从XML中读取一些值。例如,我尝试使用以下代码从XML获取ItemID,但未返回任何结果。

SELECT 
      N.C.value('itemId[1]', 'int') ItemId

  FROM [isalesystemdb].[dbo].[Test_eBay_Keyword_Transaction] 
  cross apply rawdata.nodes('/findItemsByKeywordsResponse/SearchResult/item') N(C)

感谢您的帮助! 这是XML'

<findItemsByKeywordsResponse> xmlns="http://www.ebay.com/marketplace/search/v1/services">
  <ack>Success</ack>
  <version>1.12.0</version>
  <timestamp>2013-09-03T01:10:07.967Z</timestamp>
  <searchResult count="100">
    <item>
      <itemId>231044911361</itemId>
      <title>3 X SNAKE REPELLER SOLAR POWER ULTRA SONIC LED PEST RODENT MULTI PULSE REPELLANT</title>
    </item>

</searchResult>
  <paginationOutput>
    <pageNumber>1</pageNumber>
    <entriesPerPage>100</entriesPerPage>
    <totalPages>3</totalPages>
    <totalEntries>270</totalEntries>
  </paginationOutput>
  <itemSearchURL>http://www.ebay.com.au/sch/i.html?_nkw=snake+repeller&amp;_ddo=1&amp;_ipg=100&amp;_pgn=1</itemSearchURL>
</findItemsByKeywordsResponse>'

2 个答案:

答案 0 :(得分:3)

试试这个 -

DECLARE @XML XML
SELECT @XML = '
<findItemsByKeywordsResponse xmlns="http://www.ebay.com/marketplace/search/v1/services">
    <ack>Success</ack>
    <version>1.12.0</version>
    <timestamp>2013-09-03T01:10:07.967Z</timestamp>
    <searchResult count="100">
        <item>
            <itemId>231044911361</itemId>
            <title>3 X SNAKE REPELLER SOLAR POWER ULTRA SONIC LED PEST RODENT MULTI PULSE REPELLANT</title>
        </item>
    </searchResult>
    <paginationOutput>
        <pageNumber>1</pageNumber>
        <entriesPerPage>100</entriesPerPage>
        <totalPages>3</totalPages>
        <totalEntries>270</totalEntries>
    </paginationOutput>
    <itemSearchURL>http://www.ebay.com.au/sch/i.html?_nkw=snake+repeller&amp;_ddo=1&amp;_ipg=100&amp;_pgn=1</itemSearchURL>
</findItemsByKeywordsResponse>'

;WITH XMLNAMESPACES (DEFAULT 'http://www.ebay.com/marketplace/search/v1/services')
SELECT itemId = t.c.value('itemId[1]', 'BIGINT')
FROM @XML.nodes('/findItemsByKeywordsResponse/searchResult/item') t(c)

输出 -

tt

答案 1 :(得分:3)

XML区分大小写。将/SearchResult/替换为/searchResult/