I'm trying to parse an xml file that has the contents of a simple order form. I'm comfortable with parsing an XML file which would have the contents of such:
<list>
<item>
<id>1</id>
<quantity>14</quantity>
</item>
<item>
<id>2</id>
<quantity>3</quantity>
</item>
</list>
Now I would like to be able to parse an xml file that is structured like so. This file is named "order.xml" for future reference.
<main>
<user>
<address>123 Fake Street, City, STATE, ZIP</address>
<list>
<item>
<id>1</id>
<quantity>3</quantity>
</item>
<item>
<id>3</id>
<quantity>4</quantity>
</item>
</list>
</user>
<user>
<address>246 Fake Street, City, STATE, ZIP</address>
<list>
<item>
<id>2</id>
<quantity>4</quantity>
</item>
<item>
<id>3</id>
<quantity>4</quantity>
</item>
</list>
</user>
</main>
The PHP code for which I'm using to parse the file is as so:
<?php
// load SimpleXML
$main = new SimpleXMLElement('order.xml', null, true);
$list = $main;
print("<table border = '1'>
<tr>
<th>Address</th>
<th>Item_id</th>
<th>Quantity</th>
</tr> ");
foreach($main as $user) // Loops through the users
{
print ("<tr>
<td>{$user->address}</td>");
foreach($list as $item)
{
print ("<td>{$item->id}</td>
<td>{$item->quantity}</td></tr>");
}
}
echo '</table>';
?>
so for an output I would like the PHP script to create a table something like the following, but properly formatted in an HTML table for easy viewing.:
Address Item_id Quantity
Address 1 2 3
Address 1 3 4
Address 2 1 1
Thank you very much in advance!