Quantcast
Channel: How can I keep missing XML elements from breaking my RSS-driven newsletter? - Salesforce Stack Exchange
Viewing all articles
Browse latest Browse all 2

How can I keep missing XML elements from breaking my RSS-driven newsletter?

$
0
0

I'm trying build an newsletter that populates the five most recent stories from our website.

I'm using this AMPscript to parse the XML:

    %%[Var @xml, @xml1, @titles, @authors, @author, @descs, @desc, @links, @link, @images, @image, @cnt
Set @xml1 = ContentAreaByName("My Contents\My feed") /*This line specifies the content area from which the RSS content will be pulled for the email message.*/

Set @xml = Replace(@xml1,'media:content','mediacontent')
Set @titles = BuildRowsetFromXML(@xml,"//item/title",1)
Set @descs = BuildRowsetFromXML(@xml,"//item/description",1)
Set @links = BuildRowsetFromXML(@xml,"//item/link",1)
Set @authors = BuildRowsetFromXML(@xml,"//item/author",1)
Set @images = BuildRowsetFromXML(@xml,"//item/mediacontent",1)
If RowCount(@titles) > 5 THEN
SET @rows = 5
ELSE
SET @rows = RowCount(@titles)
ENDIF
IF @rows >= 1 THEN
for @cnt = 1 to 5 do
Set @title = Field(Row(@titles,@cnt),"Value")
Set @desc = Field(Row(@descs,@cnt), "Value")
Set @link = Field(Row(@links,@cnt), "Value")
Set @author = Field(Row(@authors,@cnt), "Value")
Set @image = Field(Row(@images,@cnt), "url_att")]%%

The problem is that not every item in the RSS has a media:content element. So ExactTarget is spitting out an error. How can I modify this AMPscript to work even when one of the 5 most recent items lacks a media:content element?

Thanks!

I've tried to figure out how to put the answer provided into practice, but I don't think I understand it fully. This is what I have right now:

Set @xml = Replace(@xml1,'media:content','mediacontent')
Set @titles = BuildRowsetFromXML(@xml,"//item/title",0)
Set @descs = BuildRowsetFromXML(@xml,"//item/description",0)
Set @links = BuildRowsetFromXML(@xml,"//item/link",0)
Set @authors = BuildRowsetFromXML(@xml,"//item/author",0)
Set @images = BuildRowsetFromXML(@xml,"//item/mediacontent",0)

Set @rows = RowCount(@titles)

for @i to @rows

if RowCount(BuildRowsetFromXML(@xml,concat("//item[",@i,"]/title"),0)) > 0 then
Set @title = Field(Row(BuildRowsetFromXML(@xml,concat("//item[",@i,"]/title"),0),1),"Value")
endif

if RowCount(BuildRowsetFromXML(@xml,concat("//item[",@i,"]/description"),0)) > 0 then
Set @description = Field(Row(BuildRowsetFromXML(@xml,concat("//item[",@i,"]/description"),0),1),"Value")
endif

if RowCount(BuildRowsetFromXML(@xml,concat("//item[",@i,"]/link"),0)) > 0 then
Set @link = Field(Row(BuildRowsetFromXML(@xml,concat("//item[",@i,"]/link"),0),1),"Value")
endif

if RowCount(BuildRowsetFromXML(@xml,concat("//item[",@i,"]/author"),0)) > 0 then
Set @author = Field(Row(BuildRowsetFromXML(@xml,concat("//item[",@i,"]/author"),0),1),"Value")
endif

if RowCount(BuildRowsetFromXML(@xml,concat("//item[",@i,"]/mediacontent"),0)) > 0 then
Set @image = Field(Row(BuildRowsetFromXML(@xml,concat("//item[",@i,"]/mediacontent"),0),1),"url_att")
endif

But I don't think this is correct. Any help with what I'm misunderstanding would be greatly appreciated. Thank you.


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images