Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP xml parse

Status
Not open for further replies.

Brianfree

Programmer
Feb 6, 2008
220
GB
Hi i have an xml file with multiple tags that are the same.. how can i parse into an array? So i can display data later on in the page?

example...
Code:
<parts>
<Part>
<Part_Type>CD</Part_Type>
<Part_Price>10.00</Part_Price>
<Part_Details>REM</Part_Details>
</Part>
<Part>
<Part_Type>DVD</Part_Type>
<Part_Price>12.00</Part_Price>
<Part_Details>THE MATRIX</Part_Details>
</Part>
<Part>
<Part_Type>BOOK</Part_Type>
<Part_Price>5.00</Part_Price>
<Part_Details></Part_Details>
</Part>
</parts>

many thanks,

Brian
 
Hi

Take a look at [tt]SimpleXML[/tt] :
Code:
[blue]php >[/blue] print_r(simplexml_load_string('<parts>
[blue]php '[/blue] <Part>
[blue]php '[/blue] <Part_Type>CD</Part_Type>
[blue]php '[/blue] <Part_Price>10.00</Part_Price>
[blue]php '[/blue] <Part_Details>REM</Part_Details>
[blue]php '[/blue] </Part>
[blue]php '[/blue] <Part>
[blue]php '[/blue] <Part_Type>DVD</Part_Type>
[blue]php '[/blue] <Part_Price>12.00</Part_Price>
[blue]php '[/blue] <Part_Details>THE MATRIX</Part_Details>
[blue]php '[/blue] </Part>
[blue]php '[/blue] <Part>
[blue]php '[/blue] <Part_Type>BOOK</Part_Type>
[blue]php '[/blue] <Part_Price>5.00</Part_Price>
[blue]php '[/blue] <Part_Details></Part_Details>
[blue]php '[/blue] </Part>
[blue]php '[/blue] </parts>'));
SimpleXMLElement Object
(
    [Part] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [Part_Type] => CD
                    [Part_Price] => 10.00
                    [Part_Details] => REM
                )

            [1] => SimpleXMLElement Object
                (
                    [Part_Type] => DVD
                    [Part_Price] => 12.00
                    [Part_Details] => THE MATRIX
                )

            [2] => SimpleXMLElement Object
                (
                    [Part_Type] => BOOK
                    [Part_Price] => 5.00
                    [Part_Details] => SimpleXMLElement Object
                        (
                        )

                )

        )

)

Feherke.
 
one of the raw/native php method is as simple as this

Code:
<?php
ini_set('display_errors', true); error_reporting(E_ALL);
$string = <<<XML
<parts>
<Part>
<Part_Type>CD</Part_Type>
<Part_Price>10.00</Part_Price>
<Part_Details>REM</Part_Details>
</Part>
<Part>
<Part_Type>DVD</Part_Type>
<Part_Price>12.00</Part_Price>
<Part_Details>THE MATRIX</Part_Details>
</Part>
<Part>
<Part_Type>BOOK</Part_Type>
<Part_Price>5.00</Part_Price>
<Part_Details></Part_Details>
</Part>
</parts>
XML;
$xml = simplexml_load_string($string);
echo '<table>';
foreach($xml->Part as $part):?>
	<tr>
		<?php foreach(array('Part_Type', 'Part_Price', 'Part_Details') as $f): ?>
		<td><?php echo (string) $part->$f;?></td>
		<?php endforeach;?>
	</tr>	
<?php endforeach; ?>
</table>
 
HI, thanks for your replies... just checking our jpadies post.

Many thanks,

Brian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top