Give the following XML I am trying to split the contents of the "extra" node into four parts by the | delimiter and return the four parts in the response as chdspt(0), chdspt(1), chdspt(2) and chdspt(3). The problem I seem to have is if the XML loaded is missing anything inside the "extra" node then the whole script fails with a 500 error.
<Export>
<SAVED_EXPORT>
<id>00-6189</id>
<title>00-6189 Start Mech Switch</title>
<price>5.46 USD</price>
<extra>Male|Adult|Black|medium</extra>
</SAVED_EXPORT>
<SAVED_EXPORT>
<id>00-6190</id>
<title>00-6190 Start Mech Switch</title>
<price>5.46 USD</price>
<extra></extra>
</SAVED_EXPORT>
</Export>
Note that I removed a lot of the sibling nodes from the XML above as it is too much to post and should be unnecessary for the question at hand.
Dim xData,xNewDoc,xmldataout,xmldataout2,title,description,link,gpc,chd,chdspt
url = "http://thesite.com/v/myxml.xml"
Set xData = Server.CreateObject("Microsoft.XMLHTTP")
xData.Open "get", url, False
xData.Send
Set xNewDoc = xData.responseXML 'ResponseXml returns DOMDocument object
For Each x In xNewDoc.documentElement.selectNodes(".//SAVED_EXPORT")
Dim productid: Set productid = x.selectSingleNode("id")
Dim pt: Set pt = x.selectSingleNode("title")
Dim pds: Set pds = x.selectSingleNode("striphtml-description")
Dim pl: Set pl = x.selectSingleNode("link")
Dim pe: Set pe = x.selectSingleNode("product_type")
Dim pri: Set pri = x.selectSingleNode("price")
Dim psp: Set psp = x.selectSingleNode("sale_price")
Dim pbr: Set pbr = x.selectSingleNode("brand")
Dim pcn: Set pcn = x.selectSingleNode("condition")
Dim pex: Set pex = x.selectSingleNode("expiration_date")
Dim pwe: Set pwe = x.selectSingleNode("weight")
Dim ppn: Set ppn = x.selectSingleNode("id")
Dim pil: Set pil = x.selectSingleNode("image_link")
Dim pav: Set pav = x.selectSingleNode("availability")
Dim ppi: Set ppi = x.selectSingleNode("upc")
Dim pch: Set pch = x.selectSingleNode("extra")
title=Replace(pt.text,"&","&")
title=Replace(title,"<","<")
title=Replace(title,">",">")
description=Replace(pds.text,"&","&")
description=Replace(description,"<","<")
description=Replace(description,">",">")
link=Replace(pl.text,"&","&")
link=Replace(link,"<","<")
link=Replace(link,">",">")
gpc=Replace(pri.text,"&","&")
gpc=Replace(gpc,"<","<")
gpc=Replace(gpc,">",">")
chd = pch.text
chdspt = split(chd, "|")
xmldataout= "<item><g:id>" & productid.text & "</g:id>" & "<title>" & title & "</title>"
& "<description>" & description & "</description>" & "<link>" & link & "</link>"
& "<g:google_product_category>" & gpc & "</g:google_product_category>" & "<g:price>" & pri.text & "</g:price>" & "<g:sale_price>" & psp.text & "</g:sale_price>"
& "<g:brand>" & pbr.text & "</g:brand>" & "<g:condition>" & pcn.text & "</g:condition>"
& "<g:expiration_date>" & pex.text & "</g:expiration_date>" & "<g:shipping_weight>" & pwe.text & "</g:shipping_weight>" & "<g:mpn>" & ppn.text & "</g:mpn>"
& "<g:image_link>" & pil.text & "</g:image_link>" & "<g:availability>" & pav.text & "</g:availability>" & "<g:gtin>" & ppi.text & "</g:gtin>"
& "<g:gender>" & chdspt(0) & "</g:gender>" & "<g:age_group>" & chdspt(1) & "</g:age_group>" & "<g:color>" & chdspt(2) & "</g:color>"
& "<g:size>" & chdspt(3) & "</g:size> </item>"
xmldataout2=xmldataout2+xmldataout
Next
Response.ContentType = "text/xml"
Response.Write("<?xml version='1.0' encoding='UTF-8'?><rss version='2.0' xmlns:g='http://base.google.com/ns/1.0'><channel><title>store</title><link>http://www.thesite.com</link><description>This is a sample feed</description>" & xmldataout2 & "</channel></rss>")
Set xNewDoc = Nothing
Set xData = Nothing
Here is a completely new version of your code, using "best practices" (i.e. no manual character escaping, no building XML from strings but using a DOM instead, using current COM objects (
Microsoft.XMLHTTP
is way outdated).Note that
NewElem()
andGetText()
are convenience functions found at the end of the code.NewElem()
creates a new Element with or without namespace and adds a text value to itGetText()
searches a context (document or node) for an XPath expression and returns the text value of the first element found, or""
(i.e. a smarter.selectSingleNode()
)ASP code:
Think about using the more appropriate
application/rss+xml
content type instead oftext/xml
Just noticed that @TomaLak left out the
<?xml version='1.0' ?>
in this line of code above