Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Parse XML


Parse XML

Author
Message
chris.ross 34852
chris.ross 34852
SSC Rookie
SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)

Group: General Forum Members
Points: 33 Visits: 99
Hello! I have some XML I need to parse and am having trouble sorting out how. I will need to parse a few hundred rows at a time and each row contains XML like the following snippet:

<feed xmlns:im="http://itunes.apple.com/rss" xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
   <id>https://itunes.apple.com/AU/rss/topalbums/limit=10/xml</id>
   <title>iTunes Store: Top Albums</title>
   <entry>
      <updated>2013-08-28T03:55:45-07:00</updated>
      <title>Paradise Valley - John Mayer</title>
      <im:releaseDate label="16 Aug 2013">2013-08-16T00:00:00-07:00</im:releaseDate>
      <im:itemCount>11</im:itemCount>
   </entry>
   <entry>
      <updated>2013-08-28T03:55:45-07:00</updated>
      <title>PRISM (Deluxe Version) - Katy Perry</title>
      <rights>℗ 2013 Capitol Records, LLC</rights>
      <im:releaseDate label="21 Oct 2013">2013-10-21T00:00:00-07:00</im:releaseDate>
      <im:itemCount>16</im:itemCount>
   </entry>
</feed>

And I'd like the results for each row to return results like this:

Title            ReleaseDate       Updated       ItemCount
Paradise Valley - John Mayer      2013-08-28T03:55:45-07:00    2013-08-16T00:00:00-07:00   11
PRISM (Deluxe Version) - Katy Perry 2013-08-28T03:55:45-07:00 2013-10-21T00:00:00-07:00   16

Thanks!
Mark Cowne
Mark Cowne
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2086 Visits: 22778
Try this


DECLARE @x XML = '
<feed xmlns:im="http://itunes.apple.com/rss" xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
<id>https://itunes.apple.com/AU/rss/topalbums/limit=10/xml</id>
<title>iTunes Store: Top Albums</title>
<entry>
<updated>2013-08-28T03:55:45-07:00</updated>
<title>Paradise Valley - John Mayer</title>
<im:releaseDate label="16 Aug 2013">2013-08-16T00:00:00-07:00</im:releaseDate>
<im:itemCount>11</im:itemCount>
</entry>
<entry>
<updated>2013-08-28T03:55:45-07:00</updated>
<title>PRISM (Deluxe Version) - Katy Perry</title>
<rights>? 2013 Capitol Records, LLC</rights>
<im:releaseDate label="21 Oct 2013">2013-10-21T00:00:00-07:00</im:releaseDate>
<im:itemCount>16</im:itemCount>
</entry>
</feed>';

WITH XMLNAMESPACES(default 'http://www.w3.org/2005/Atom', 'http://itunes.apple.com/rss' AS im)
SELECT x.r.value('(title/text())[1]','VARCHAR(30)') AS Title,
x.r.value('(im:releaseDate/text())[1]','DATETIME') AS ReleaseDate,
x.r.value('(updated/text())[1]','DATETIME') AS Updated,
x.r.value('(im:itemCount/text())[1]','INT') AS ItemCount
FROM @x.nodes('/feed/entry') AS x(r)



____________________________________________________

Deja View - The strange feeling that somewhere, sometime you've optimised this query before

How to get the best help on a forum

http://www.sqlservercentral.com/articles/Best+Practices/61537




chris.ross 34852
chris.ross 34852
SSC Rookie
SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)

Group: General Forum Members
Points: 33 Visits: 99
Thank you!!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search