0

I have a Partner sending some data to a Triggered Email/Data Extension which includes a column for XML data.

The trigger is erroring and not allowing the data to get to the Data Extension.

Below is what they are sending over. Is the formatting of the XML incorrect?

XML data that I've used in the past in Data Extensions starts with....

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

This version of XML starts with... {"XML":

Could that be it?

{"To": {"SubscriberKey": "test@acme.com", "ContactAttributes": {"SubscriberAttributes": **{"XML":** "<root><ITEM><ITEM_PRODUCT_NAME>dark chocolate nuts & sea salt</ITEM_PRODUCT_NAME><ITEM_PRODUCT_SKU>15127</ITEM_PRODUCT_SKU><ITEM_PRODUCT_URL>https://www.test.com/15127.html</ITEM_PRODUCT_URL><ITEM_PRODUCT_IMG_URL>https://www.test.com/images/nuts.png</ITEM_PRODUCT_IMG_URL><ITEM_QTY>1</ITEM_QTY><ITEM_SUBSCRIPTION_PRICE>12.59</ITEM_SUBSCRIPTION_PRICE><ITEM_TOTAL>12.59</ITEM_TOTAL></ITEM><ITEM><ITEM_PRODUCT_NAME>crunchy peanut butter</ITEM_PRODUCT_NAME><ITEM_PRODUCT_SKU>41800</ITEM_PRODUCT_SKU><ITEM_PRODUCT_URL>https://www.test.com/images/butter.png</ITEM_PRODUCT_IMG_URL><ITEM_QTY>1</ITEM_QTY><ITEM_SUBSCRIPTION_PRICE>11.37</ITEM_SUBSCRIPTION_PRICE><ITEM_TOTAL>9.55</ITEM_TOTAL></ITEM><ITEM><ITEM_PRODUCT_NAME>pear cinnamon & pecan</ITEM_PRODUCT_NAME><ITEM_PRODUCT_SKU>17227</ITEM_PRODUCT_SKU><ITEM_PRODUCT_URL>https://www.test.com</ITEM_PRODUCT_URL><ITEM_PRODUCT_IMG_URL>https://www.test.com/images/apples.png</ITEM_PRODUCT_IMG_URL><ITEM_QTY>1</ITEM_QTY><ITEM_SUBSCRIPTION_PRICE>12.59</ITEM_SUBSCRIPTION_PRICE><ITEM_TOTAL>12.59</ITEM_TOTAL></ITEM></root>", "SHIPPING_LNAME": "Tester", "FIRST_NAME": "Test", "LAST_NAME": "Tester", "ORDER_TOTAL_SAVINGS": -5.97, "MERCHANT_ORDER_ID": "1810002352", "SHIPPING_ADDRESS_STATE": "NY", "ORDER_PLACE_DATE_LONG": "Fri 11 January 2019", "SHIPPING_ADDRESS_ZIP": "10004", "ORDER_SHIPPING": 0.00, "CARD_TYPE": "Visa", "ORDER_SUBTOTAL": 47.32, "ORDER_PLACE_DATE_SHORT": "01/11/2019", "SHIPPING_FNAME": "Test", "SHIPPING_ADDRESS_TOWN": "new york", "ORDER_TOTAL": 47.32, "CARD_EXPIRE": "12/2027", "CARD_HOLDER": "Test Tester", "SHIPPING_ADDRESS_1": "75 broad st", "SHIPPING_ADDRESS_2": "fl 23"}}, "Address": "acme@test.com"}}

AMPSCRIPT IN EMAIL Calling the XML....

%%[ 

var @LOOPINGAPPENDIX 
set @LOOPINGAPPENDIX = AttributeValue("LOOPINGAPPENDIX") 
set @productRows = BuildRowSetFromXML(@LOOPINGAPPENDIX, "/root/ITEM", 0)

]%% 

%%[ 

for @j = 1 to RowCount(@productRows) do 

    set @productXML = Concat("", Field(Row(@productRows, @j), "XML"), "") 
    set @ITEMPRODUCTNAME = Trim(Field(Row(BuildRowSetFromXML(@LOOPINGAPPENDIX, "/root/ITEM/ITEM_PRODUCT_NAME", 0), 1), "Value")) 
    set @ITEMPRODUCTSKU = Trim(Field(Row(BuildRowSetFromXML(@LOOPINGAPPENDIX, "/root/ITEM/ITEM_PRODUCT_SKU", 0), 1), "Value")) 
    set @ITEMPRODUCTURL = Trim(Field(Row(BuildRowSetFromXML(@LOOPINGAPPENDIX, "/root/ITEM/ITEM_PRODUCT_URL", 0), 1), "Value")) 
    set @ITEMPRODUCTIMGURL = Trim(Field(Row(BuildRowSetFromXML(@LOOPINGAPPENDIX, "/root/ITEM/ITEM_PRODUCT_IMG_URL", 0), 1), "Value")) 
    set @ITEMQTY = Trim(Field(Row(BuildRowSetFromXML(@LOOPINGAPPENDIX, "/root/ITEM/ITEM_QTY", 0), 1), "Value")) 
    set @ITEMSUBSCRIPTIONPRICE = Trim(Field(Row(BuildRowSetFromXML(@LOOPINGAPPENDIX, "/root/ITEM/ITEM_SUBSCRIPTION_PRICE", 0), 1), "Value")) 
    set @ITEMTOTAL = Trim(Field(Row(BuildRowSetFromXML(@LOOPINGAPPENDIX, "/root/ITEM/ITEM_TOTAL", 0), 1), "Value")) 
    set @rowColor = Iif(Mod(@j,2) == 0, "#efefef", "#ffffff") 

]%% 

%%[endif]%% 

%%[next @j]%%
1

Here's a defensive way to parse XML with AMPScript.

The idea is that you need to check the rowcount to ensure XML nodes exist before you try to pull values out of them.

%%[

var @xml, @isXML, @nodes, @rowCount

set @xml = ""
set @xml = concat(@xml, '<?xml version="1.0" encoding="UTF-8"?>')
set @xml = concat(@xml, '<cart>')
set @xml = concat(@xml, '  <items>')
set @xml = concat(@xml, '    <item>')
set @xml = concat(@xml, '       <sku><![CDATA[123]]></sku>')
set @xml = concat(@xml, '       <name><![CDATA[Square]]></name>')
set @xml = concat(@xml, '       <url><![CDATA[https://limedash.com?sku=123]]></url>')
set @xml = concat(@xml, '    </item>')
set @xml = concat(@xml, '    <item>')
set @xml = concat(@xml, '        <sku><![CDATA[456]]></sku>')
set @xml = concat(@xml, '        <name><![CDATA[Circle]]></name>')
set @xml = concat(@xml, '        <url><![CDATA[https://limedash.com?sku=456]]></url>')
set @xml = concat(@xml, '    </item>')
set @xml = concat(@xml, '    <item>')
set @xml = concat(@xml, '        <sku><![CDATA[789]]></sku>')
set @xml = concat(@xml, '        <name><![CDATA[Triangle]]></name>')
set @xml = concat(@xml, '        <url><![CDATA[https://limedash.com?sku=789]]></url>')
set @xml = concat(@xml, '    </item>')
set @xml = concat(@xml, '  </items>')
set @xml = concat(@xml, '</cart> ')

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 @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(@name)=%% (%%=v(@sku)=%%)</a>

      %%[

      endif

    next @i

  else

   output(concat("<br>no products found"))

  endif

else

  output(concat("<br>no XML found"))

endif

]%%

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.