Jump to content
Sign in to follow this  
aarondurber01

Using PHP to loop through XML nodes

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: 

Share this post


Link to post
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";
}

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

I don't see the connection. What does post metadata have to do with an XML feed? I thought you were trying to load an XML feed and display data from it? What does WordPress have to do with it?

Share this post


Link to post
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

Share this post


Link to post
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?

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites

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.

Sign in to follow this  

×
×
  • 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.