0

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

5
  • What do you mean by "reference the xml by its link"? Please update your question to include the desired output. Commented May 30, 2022 at 23:17
  • Hi, Adam I have updated the question adding more about what I want to do, thanks in advance! Commented Jun 1, 2022 at 18:06
  • I'm not following. What do you want to do with the link? In the example the XML URL can be dynamic if you want -- the @xml can reference whatever returns XML. The url field is the link from the XML. Commented Jun 2, 2022 at 2:21
  • There are also plenty of examples of parsing XML around here. Commented Jun 2, 2022 at 2:23
  • In a nutshell I want to read the xml link in my ampscript to then execute the rest of my code, I will look in others questions as well, thanks!! Commented Jun 6, 2022 at 19:53

0

You must log in to answer this question.

Browse other questions tagged .