I have my xml in a link and I would like to parse ampscript, how could I read the XML in my ampscript? XML -
<?xml version="1.0" encoding="UTF-8"?>
<cart>
<items>
<item>
<sku><![CDATA[123]]></sku>
<name><![CDATA[Square]]></name>
<url><![CDATA[https://example.com?sku=123]]></url>
</item>
<item>
<sku><![CDATA[456]]></sku>
<name><![CDATA[Circle]]></name>
<url><![CDATA[https://example.com?sku=456]]></url>
</item>
<item>
<sku><![CDATA[789]]></sku>
<name><![CDATA[Triangle]]></name>
<url><![CDATA[https://example.com?sku=789]]></url>
</item>
</items>
</cart>
What did i try :
%%[
var @xml, @nodes, @rowCount
set @xml = HTTPGet('http://www.example.com')
if indexOf(@xml,"<cart>") > 0 then
set @nodes = BuildRowsetFromXML(@xml,"/cart/items/item",0)
set @rowCount = rowcount(@nodes)
if @rowCount > 0 then
for @i = 1 to @rowCount do
var @nodepath
var @sku
var @name
var @url
set @nodepath = concat("/cart/items/item[",@i,"]/")
if rowcount(BuildRowsetFromXML(@xml,concat(@nodepath,"sku"))) > 0 then
set @sku = Field(Row(BuildRowsetFromXML(@xml,concat(@nodepath,"sku"),0),1),'Value')
endif
if rowcount(BuildRowsetFromXML(@xml,concat(@nodepath,"name"))) > 0 then
set @name = Field(Row(BuildRowsetFromXML(@xml,concat(@nodepath,"name"),0),1),'Value')
endif
if rowcount(BuildRowsetFromXML(@xml,concat(@nodepath,"url"))) > 0 then
set @url = Field(Row(BuildRowsetFromXML(@xml,concat(@nodepath,"url"),0),1),'Value')
endif
if not empty(@sku) and not empty(@name) and not empty(@url) then
]%%
<br><a href="%%=redirectto(@url)=%%">%%=v(@sku)=%%</a> - <a href="%%=redirectto(@url)=%%">%%=v(@name)=%%</a>
%%[
endif
next @i
else
outputline(concat("<br>no products found"))
endif
else
outputline(concat("<br>no XML found"))
endif
]%%
And the result is - no products found
I would like to read the xml by the link in the ampscript and the output be the content from the link but already parsed
@xml
can reference whatever returns XML. Theurl
field is the link from the XML.