I’m parsing an xml to append() to a table and the problem is that the xml come in with diferent columns depending on the product. So if it’s product: A, give me 4 columns, Product: B gives me 3 columns, product: C gives 2 columns…and so on. Also, the column header are in the product node.
Is there a way to set how many columns come in, set column headers and iterate through?
<rates>
<response_code>0</response_code>
<countryID>36</countryID>
<country_name>Brazil</country_name>
<destination_area_code>55</destination_area_code>
<destination_description>Brazil</destination_description>
<destination_currency>USD</destination_currency>
<currency_symbol>$</currency_symbol>
<product columns="4">
<destination_rate_1>0.05920</destination_rate_1>
<product_name_1>Global Landline</product_name_1>
<destination_rate_2>0.14080</destination_rate_2>
<product_name_2>National Landline</product_name_2>
<destination_rate_3>0.06920</destination_rate_3>
<product_name_3>Local Landline</product_name_3>
<destination_rate_4>0.06920</destination_rate_4>
<product_name_4>Global LD</product_name_4>
</product>
</rates>
<rates>
<response_code>0</response_code>
<countryID>23</countryID>
<country_name>Dubai</country_name>
<destination_area_code>39</destination_area_code>
<destination_description>Dubai</destination_description>
<destination_currency>USD</destination_currency>
<currency_symbol>$</currency_symbol>
<product columns="4">
<destination_rate_1>0.06950</destination_rate_1>
<product_name_1>Global Landline</product_name_1>
<destination_rate_2>0.13820</destination_rate_2>
<product_name_2>National Landline</product_name_2>
<destination_rate_3>0.04520</destination_rate_3>
<product_name_3>Local Landline</product_name_3>
<destination_rate_4>0.05830</destination_rate_4>
<product_name_4>Global LD</product_name_4>
</product>
</rates>
The table end result
This is the result table for this product. The xml brings in 4 columns. If I select a different product, the xml would bring in 2 or 3..
So what you really need to do is destroy the entire table and create a new one, but reuse the same styles.
As for how many columns there are, you have that as an attribute in your XML already. Just
parseIntthat attribute, or use$(node).children().lengthto count them explicitly.