
var i;
for(i = 0;i < products.length;i++)
if(products[i] != null)
xmlCode += products[i].toXML();
return element(“products”,””,xmlCode);
}
Notice that this approach is recursive. Product implements its toXML()
method partly by serializing the list of Price and wrapping it in a product
element.
function product_toXML()
{
var result = element(“name”,””,escapeXML(this.name)),
i;
for(i = 0;i < this.prices.length;i++)
result += this.prices[i].toXML();
return element(“product”,””,result);
}
function price_toXML()
{
return element(“price”,
“currency=\”” + this.currency + “\””,
escapeXML(this.amount));
}
XML is a convenient format because elements can nest in a way that ...