Populate an HTML table with XML data



I would like to populate a table with data from an XML file.

I was thinking along the lines of:


1) create XML data:



<menu>
<pizza>
<regular>
<item name="Tomato Cheese">Tomato Cheese
<price small="small">1</price>
<price large="large">2</price>
<description>A</description>
</item>
<item name="Onion">Onion
<price small="small">3</price>
<price large="large">4</price>
<description>B</description>
</item>
</regular>
</pizza>
</menu>


2) create the table in HTML:



<table border="1">
<thead>
<tr>
<th rowspan="2">Item </th>
<th rowspan="2">Description</th>
<th colspan="2">Price</th>
</tr>
<tr>
<th>Small</th>
<th>Large</th>
</tr>
</thead>
<tbody>


3)use a foreach statement on an xml query:



foreach ($xml->xpath('/menu/pizza/descendant::item') as $item)
{
print "<tr><td>".$item."</td><tr>" ;
}


This works great for the first row, but I can't figure out how to populate the rest of the columns.


No comments:

Post a Comment