Jump to content

Help with XML Parser


petschephp

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>';

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.