Jump to content

Recommended Posts

I am trying to parse a XML file using SimpleXML, but I can't figure out how to display <Category> name and each <feature>

 

Here's a example of the XML file.

<Vehicle>

<Description>To set an appointment callor email </Description>

<Features>

<Category Name="Comfort">

<Feature>Front Air Conditioning</Feature>

<Feature>Front Air Conditioning Zones: Single</Feature>

<Feature>Front Air Conditioning: Climate Control</Feature>

</Category>

</Features>

</Vehicle>

Link to comment
https://forums.phpfreaks.com/topic/247737-help-with-xml-parser/
Share on other sites

What exactly is the problem?  Do you have any code?

 

Example #4 shows you how to loop through:

http://us3.php.net/manual/en/simplexml.examples-basic.php

 

For the name you need the attribute:

http://us3.php.net/manual/en/simplexmlelement.attributes.php

 

 

Here is the code. It works, but I just can't figure out how to get the features to display.

 

// load SimpleXML
$cars = new SimpleXMLElement('inventory.xml', null, true);

echo <<<EOF
<table>
        <tr>
                <th>ID</th>
                <th>Stock Number</th>
                <th>InventoryDate</th>
                <th>CreatedDate</th>
                <th>VIN</th>
			<th>TYPE</th>
			<td>Features</td>
        </tr>

EOF;
foreach($cars as $car) // loop through our cars
{
        echo <<<EOF
        <tr>
                <td>{$car->ID}</td>
                <td>{$car->StockNum}</td>
                <td>{$car->InventoryDate}</td>
                <td>{$car->CreatedDate}</td>
                <td>{$car->VIN}</td>
			<td>{$car->TYPE}</td>
			<td></td>
        </tr>

EOF;
}
echo '</table>';

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.