Read first element+attributes and secondary elements+attributes of a XML file on PHP5



Actually I want my page to read this XML file on PHP5.


But I need this page to read the product names as an array just like this:


(pasta1, pasta2) / (meat1, meat 2)


I have this example as my samples.xml file:



<sample amount="5" name="Pasta" dest="pasta/sample_pasta.lua">
<product name="pasta1"/>
<product name="pasta2"/>
</sample>
...
<sample amount="18" name="Meat" dest="pasta/sample_meat.lua">
<product name="meat1"/>
<product name="meat2"/>
</sample>


And there is my php code:



<?php
echo '<table><tr><td>Name</td><td>Amount</td><td>Product</td></tr>';
$reader = new XMLReader();
if (!$reader->open("samples.xml")) {
die("Failed to open 'samples.xml'");
}
while($reader->read()) {
if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == 'sample') {
$amount = $reader->getAttribute('amount');
$name = $reader->getAttribute('name');
echo '<tr><td>'.$name.'</td><td>'.$amount.'</td><td>---?[array result here]?---</td></tr>';
}
echo '</table>';
?>


And this is what my script prints on page:



Name | Amount | Product


Pasta | 5 | ---?[array result here]?---


Meat | 18 | ---?[array result here]?---



Please, any information would be helpful!!!


No comments:

Post a Comment