1

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

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]?---

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

Name | Amount | Product

Pasta | 5 | pasta1, pasta2

Meat | 18 | meat1, meat2

Please, any information would be helpful!!!

Kevin
  • 41,694
  • 12
  • 53
  • 70
user3050478
  • 272
  • 3
  • 19

1 Answers1

1

Actually I'm quite used to SimpleXMLElement, but this should crack it.

echo '<table cellpadding="10"><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');
        $sample = $reader->expand();
        $products = array();
        foreach($sample->childNodes as $product) {
            if(get_class($product) != 'DOMElement') continue;
            $products[] = (string) $product->getAttribute('name');
        }

        echo '<tr><td>'.$name.'</td><td>'.$amount.'</td><td>'.implode(', ', $products).'</td></tr>';
    }
}
echo '</table>';

Upon looking into the manual, you need to expand it to get sample, loop the childnodes (which is the products), and again use ->getAttribute. Gather the attributes in an array, then implode them.

Here is the SimpleXMLElement version (same concept acutally):

$xml = simplexml_load_file('samples.xml');
echo '<table cellpadding="10"><tr><td>Name</td><td>Amount</td><td>Product</td></tr>';
foreach($xml->sample as $sample) {
    $name = (string) $sample->attributes()->name;
    $amount = (string) $sample->attributes()->amount;
    $products = array();
    foreach($sample->product as $product) {
        $products[] = (string) $product->attributes()->name;
    }
    $products = implode(', ', $products);
    echo "
        <tr>
            <td>$name</td>
            <td>$amount</td>
            <td>$products</td>
        </tr>
    ";
}
echo '</table>';
Kevin
  • 41,694
  • 12
  • 53
  • 70
  • Oh my Gosh!!! Thank you so much!!! It really worked! I really appreciate it, and now I can study on your code :) Thanks – user3050478 Sep 12 '14 at 14:02