0

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!

1
  • So relieved to open this question and see that you're using the right tool for the job. Commented May 12, 2011 at 23:22

1 Answer 1

1
<?php
    // load SimpleXML
    $main = new SimpleXMLElement('order.xml', null, true);
    print("<table border = '1'>
    <tr>
        <th>Address</th>
        <th>Item_id</th>
        <th>Quantity</th>
    </tr> ");
    foreach($main->user as $user) // Loops through the users
    {
        print ("<tr>
            <td>{$user->address}</td>");
        foreach($user->item as $item)
        {
            print ("<td>{$item->id}</td>
        <td>{$item->quantity}</td></tr>");
        }
    }
    echo '</table>';
?>
Sign up to request clarification or add additional context in comments.

4 Comments

For some reason the script will display the address of each row but it will refuses to display neither the item id nor the quantity. I'm a bit stumped on this
It should probably be $user->list->item instead of $user->item
James has it right, but there is just a small oversight. $user does not have an item property, only a list property. So before the inner for, you need $list = $user->list. Then the for becomes foreach($list->item as $item) ...
Aye I wasn't following the path all the way. Also had the change the list tags to something else since list is a reserved word in PHP. Thank you very much!

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.