shuka shuka - 3 months ago 12
PHP Question

importing xml feed with simplexml

I have being learning how to use the simplexmlelement function in php and all has being going fine until now. I have managed to get all content from the feed part from one section of the xml

[paragraphs] => SimpleXMLElement Object ( [paragraph] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 1 [type] => 0 ) [name] => Ground Floor - Private Entrance Hall [file] => SimpleXMLElement Object ( ) [dimensions] => SimpleXMLElement Object ( [metric] => 3.94m max x 2.44m max (inc stairs) [imperial] => 12'11 max x 8'0 max (inc stairs) [mixed] => 3.94m max x 2.44m max (inc stairs) (12'11 max x 8' ) [text] => Private entrance with composite entrance door opening into the ground floor section of the property. The entrance hall has a radiator and doors off to both a ground floor w.c. and Bedroom 2. Stairs also rise from the entrance hall to the first floor. ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 2 [type] => 0 ) [name] => Ground Floor WC [file] => SimpleXMLElement Object ( ) [dimensions] => SimpleXMLElement Object ( [metric] => 1.45m x 1.17m [imperial] => 4'9 x 3'10 [mixed] => 1.45m x 1.17m (4'9 x 3'10) ) [text] => Serving the adjacent ground floor bedroom with close coupled w.c. and ) [2] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 3 [type] => 0 ) [name] => Bedroom 2 [file] => SimpleXMLElement Object ( ) [dimensions] => SimpleXMLElement Object ( [metric] => 2.44m x 2.11m [imperial] => 8'0 x 6'11 [mixed] => 2.44m x 2.11m (8'0 x 6'11) ) [text] => With double glazed window to rear. Radiator. ) [3] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 4 [type] => 0 ) [name] => SimpleXMLElement Object ( ) [file] => SimpleXMLElement Object ( ) [dimensions] => SimpleXMLElement Object ( [metric] => SimpleXMLElement Object ( ) [imperial] => SimpleXMLElement Object ( ) [mixed] => SimpleXMLElement Object ( ) ) [text] => Returning to the hallway stairs rise to the first floor landing. ) [4] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 5 [type] => 0 ) [name] => First Floor - Landing [file] => SimpleXMLElement Object ( ) [dimensions] => SimpleXMLElement Object ( [metric] => SimpleXMLElement Object ( ) [imperial] => SimpleXMLElement Object ( ) [mixed] => SimpleXMLElement Object ( ) ) [text] => with doors off to the following first floor accommodation ) [5] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 6 [type] => 0 ) [name] => Bedroom 1 [file] => SimpleXMLElement Object ( ) [dimensions] => SimpleXMLElement Object ( [metric] => 2.67m x 2.49m (plus built in wardrobes) [imperial] => 8'9 x 8'2 (plus built in wardrobes) [mixed] => 2.67m x 2.49m (plus built in wardrobes) (8'9 x 8'2 ) [text] => A well proportioned double bedroom with sliding doors to two sets of built in wardrobes. Double glazed window to rear. Radiator. ) [6] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 7 [type] => 0 ) [name] => Bathroom [file] => SimpleXMLElement Object ( ) [dimensions] => SimpleXMLElement Object ( [metric] => 2.11m x 1.68m [imperial] => 6'11 x 5'6 [mixed] => 2.11m x 1.68m (6'11 x 5'6) ) [text] => Fitted with a three piece suite comprising bath with shower over and screen to side. Pedestal wash hand basin and close coupled w.c. Radiator. Obscured glazed, double glazed window. Part tiling to walls. Extractor fan. Spot lights inset to ceiling. ) [7] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 8 [type] => 0 ) [name] => Sitting / Dining Room [file] => SimpleXMLElement Object ( ) [dimensions] => SimpleXMLElement Object ( [metric] => 6.05m x 3.07m [imperial] => 19'10 x 10'1 [mixed] => 6.05m x 3.07m (19'10 x 10'1) ) [text] => An excellent dual purpose reception room of surprising proportions with ample space for both seating and dining in comfort. The room has good natural light entering the room through PVCu double glazed double doors to a "Juliette" balcony and also through a double glazed window to the rear. TV aerial point. Two radiators. Arch through to the kitchen. ) [8] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 9 [type] => 0 ) [name] => Kitchen [file] => SimpleXMLElement Object ( ) [dimensions] => SimpleXMLElement Object ( [metric] => 2.34m x 2.21m [imperial] => 7'8 x 7'3 [mixed] => 2.34m x 2.21m (7'8 x 7'3 ) ) [text] => Fitted with a range of both base and eye level storage units as well as built in appliances. The base level units being surmounted by rolled edge work surfaces. Complementary splash back tiling. Inset stainless steel sink unit with mixer tap. Built in oven above which is a four ring gas hob with fitted extractor hood over. Wall mounted gas boiler. Space for white goods, plumbing for automatic washing machine. ) [9] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 10 [type] => 0 ) [name] => Outside [file] => SimpleXMLElement Object ( ) [dimensions] => SimpleXMLElement Object ( [metric] => SimpleXMLElement Object ( ) [imperial] => SimpleXMLElement Object ( ) [mixed] => SimpleXMLElement Object ( ) ) [text] => To the front of the property there is a designated car parking space. ) [10] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 11 [type] => 0 ) [name] => Further Information [file] => SimpleXMLElement Object ( ) [dimensions] => SimpleXMLElement Object ( [metric] => SimpleXMLElement Object ( ) [imperial] => SimpleXMLElement Object ( ) [mixed] => SimpleXMLElement Object ( ) ) [text] => Any fixtures and fittings not mentioned in these details are excluded from the sale price. No services or appliances which may have been included in these details have been tested and therefore cannot be guaranteed to be in good working order. Nothing therein shall be a warranty or condition and neither the Vendor nor ourselves will be liable to the Purchaser in respect of any mis-statement or misrepresentation made at or before the date hereof by the Vendor, his servants or agents or otherwise. COUNCIL TAX BAND (A) : EPC RATING C (75): SERVICES: All mains services are connected to the property. VIEWING:By telephone appointment with ) [11] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 12 [type] => 0 ) [name] => Personal Interest [file] => SimpleXMLElement Object ( ) [dimensions] => SimpleXMLElement Object ( [metric] => SimpleXMLElement Object ( ) [imperial] => SimpleXMLElement Object ( ) [mixed] => SimpleXMLElement Object ( ) ) [text] => We are obliged to inform you that this property is owned by a director/senior partner of. ) ) )


So I have tried different ways to get the values in to a variable but am getting an error: Parse error: syntax error, unexpected '0' (T_LNUMBER), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$'

I have tired the following

$feed = file_get_contents('http://mysite.co.uk/inc/feed.xml');
$xml = simplexml_load_string($feed);
echo 'Desc: ' . $xml->paragraphys->paragraph->0->id;
echo 'Desc: ' . $xml->paragraphys->paragraph->$0->id;


I have just tried a foreach loop but that is not working either

foreach($xml->paragraphys->paragraph as $p) {
$paragraph = new Paragraph;
echo '<br/>Par: ' . $p->id;
}


Any help would be very much appreciated
Cheers
Shuka

Answer
<?php
echo 'Desc:'.(string)$xml->paragraphs->paragraph[0]->id;
?>