Jump to content

Using PHP to loop through XML nodes


aarondurber01

Recommended Posts

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 am not a php expert, I'd appreciate any help or guidance. :happy-04: 

Link to comment
Share on other sites

Use SimpleXML.

 

Example:

$xml = new SimpleXMLElement("URL", 0, true);
foreach ($xml->property as $property) {
	// $node["key"] for attributes
	$reference = (string)$property["reference"];
	// $node->name for sub nodes
	$type = (string)$property->property_type;
	$street = (string)$property->street;
	// multiple elements are accessed by their shared name
	$rooms = count($property->rooms->room); // room is basically an array of <room>s

	echo "Property {$reference} is a {$type} on {$street} with {$rooms} room(s).\n";
}
Link to comment
Share on other sites

Still struggling.

 

i am using wordpress 4.4, an expert agent feed and expert agent plugin to deliver the feed.

 

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.
Link to comment
Share on other sites

my fault for bad explanations, new to this

 

I have a clients website that runs wordpress 4.4, it uses an expert agent feed and the all in one import wordpress plugin to import the feed. I then need to display the node details. i have used the code above after a google search. to reiterate i am new to this and not an expert in PHP or xml.

 

thanks

Link to comment
Share on other sites

This might be turning into a "how do I do $x in WordPress" question...

 

How much did you copy and where did you put it? Do you know what $post is? Is it coming from the imported data you/WP got from the XML? That code you have now: what does it do now and what is it supposed to be doing instead?

Link to comment
Share on other sites

i got the code from here

 

https://perishablepress.com/wordpress-custom-fields-tips-tricks/ 

 

$post is the variable for the post item/page, no its from the standard wordpress install, i think.

 

the code pull the data from the node entrance_hall_name (which i have named)

 

entrance_hall_name maps to the rooms node, room name attribute and displays on screen.

 

Am i still explaining myself really badly?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.