Using PHP to loop through XML nodes
-
I am using an expert agent XML feed to populate a clients estate agent website.
Below is a snap shot of the nodes. I wish to loop through the rooms node using PHP to display the room name, then the size, then the description. Is there an easy way/simple solution to accomplish this within PHP?
<property reference="xxxxx"> <main_advert></main_advert> <street>Kenilworth Walk</street> <brochure>xxxxx</brochure> <property_type>House</property_type> <rooms> <room name="Entrance hall"> <measurement_text/> <description> with radiator, carpet. </description> </room> <room name="Separate WC"> <measurement_text/> <description> with low level WC, hand basin, double glazed UPVC window, radiator, carpet. </description> </room> <room name="Lounge"> <measurement_text> 13' 10'' x 15' 8'' (4.21m x 4.77m) </measurement_text> <description> with double glazed UPVC french doors and windows to garden, carpet, two radiators. </description> </room> </rooms> </property>
I need to query the rooms node (as above) each room has a size and description node. i would like to display this data on a wordpress post page. i can pull individual node details and sub nodes using code i found with google search.
<?php if(get_post_meta($post->ID, 'entrance_hall_name', true) || // this is what i have named the node for room name get_post_meta($post->ID, 'entrance_hall_desc', true) || // this is what i have named the node for room description get_post_meta($post->ID, 'entrance_hall_size', true) || // this is what i have named the node for room size ): ?> <?php if(get_post_meta($post->ID, 'entrance_hall_name', true)): ?> <b><?php echo get_post_meta($post->ID, 'entrance_hall_name', true); ?></b> <br> <?php if(get_post_meta($post->ID, 'entrance_hall_desc', true)): ?> <?php echo get_post_meta($post->ID, 'entrance_hall_desc', true); ?> <?php endif; ?> <br><br> <?php if(get_post_meta($post->ID, 'entrance_hall_size', true)): ?> <?php echo get_post_meta($post->ID, 'entrance_hall_size', true); ?> <? php echo "<br><br>"; ?> <?php endif; ?> <?php endif; ?> <?php if(get_post_meta($post->ID, 'sep_wc_name', true)): ?> <?php echo get_post_meta($post->ID, 'sep_wc_name', true); ?> <?php endif; ?> <br><br> <?php endif; ?>
I want to loop through the rooms node returning the values for those that are there, not all rooms have a size
all help appreciated.
- The topic ‘Using PHP to loop through XML nodes’ is closed to new replies.