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!!!

2
  • 1
    this should be easier using SimpleXMLElement Commented Sep 12, 2014 at 13:51
  • how can I use the simpleXMLElement to read the first and second elements with their attributes? And thank you for answering!!! Commented Sep 12, 2014 at 13:55

1 Answer 1

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>';
Sign up to request clarification or add additional context in comments.

1 Comment

Oh my Gosh!!! Thank you so much!!! It really worked! I really appreciate it, and now I can study on your code :) Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.