XML parsing with PHP (should be easy)

Soldato
Joined
28 Dec 2004
Posts
7,620
Location
Derry
I've been tasked with parsing and displaying an XML file using PHP, sounds worse than it is but I'm stumped by something in the format of the actual XML file

The PHP is simple enough:

Code:
<?php
$xmlFileData = file_get_contents("data.xml");

$xmlData = new SimpleXMLElement($xmlFileData);
foreach($xmlData->property as $test) {
	print($test->id) . "<br><br>";
	print($test->description) . "<br><br>";
	
}
?>

However, it only shows the "description", not the ID, the XML file is in the format:

<property id="xx">
<description>blah blah blah</description>
</property>

if the ID was it's own seperate element it'd show no problem but because it's in that top tag, it doesn't.

I know one of you genius's will look at it and laugh ;)
 
You want the attribute not the child so

PHP:
$test->attributes()->id

Might work. Not done any PHP for yonks though :D
 
GeX: that's not correct. You should take it to your own thread anyway as it is completely off topic to this thread.

Cuchulain: MDrX has it covered. :)
 
Back
Top Bottom