Saturday, 15 February 2014

read xml in php -



read xml in php -

i want read next construction in php.

<listing> <unit_type> 1 sleeping room </unit_type> <facilities> <facility>balcony</facility> <facility>basement parking</facility> <facility>bbq area</facility> <facility>built in wardrobes</facility> <facility>covered parking</facility> </facilities> </listing>

following code.

$feeds = new domdocument(); $feeds->load("propspace.xml"); $properties = $feeds->getelementsbytagname("listing"); foreach( $properties $property ){ $unittype_tag = $property->getelementsbytagname("unit_type"); $unit_type = $unittype_tag->item(0)->nodevalue; }

the number of <facility> tag can variable.

any help appreciated.

to read facilities, phone call getelementsbytagname("facility") on $property:

$feeds = new domdocument(); $feeds->load("propspace.xml"); $properties = $feeds->getelementsbytagname("listing"); foreach( $properties $property ){ $unittype_tag = $property->getelementsbytagname("unit_type"); $unit_type = $unittype_tag->item(0)->nodevalue; foreach($property->getelementsbytagname("facility") $f){ echo $f->nodevalue . "\n"; } }

outputs

balcony basement parking bbq area built in wardrobes covered parking

demo

php xml domdocument

No comments:

Post a Comment