Get XML element based on attribute value with php from multiple xmlfeeds

admin

Administrator
Staff member
First of all, i've got a frontpage with multiple products from multiple external xml feeds (24 different feeds at the moment), 1 product per feed. Some attributes have a different name for the same thing, for example:

<strong>one example feed</strong>

Code:
&lt;products&gt;
&lt;product&gt;
&lt;productID&gt;4943&lt;/productID&gt;
&lt;name&gt;Inbouwspot&lt;/name&gt;
&lt;price currency="EUR"&gt;199.00&lt;/price&gt;
&lt;productURL&gt;http://www.example.com&lt;/productURL&gt;
&lt;imageURL&gt;http://static.example.com/45928-1.jpg&lt;/imageURL&gt;
&lt;description&gt;&lt;![CDATA[Incl. BTW!! Verkrijgbaar in een voordeelset.]]&gt;&lt;/description&gt;
&lt;categories&gt;
&lt;category path="Aanbiedingen"&gt;Aanbiedingen&lt;/category&gt;
&lt;category path="Projectverlichting"&gt;Projectverlichting&lt;/category&gt;
&lt;/categories&gt;
&lt;additional&gt;
&lt;field name="fromPrice"&gt;395.00&lt;/field&gt;
&lt;field name="discount"&gt;196.00&lt;/field&gt;
&lt;field name="deliveryCosts"&gt;&lt;/field&gt;
&lt;field name="deliveryTime"&gt;3-5 dagen&lt;/field&gt;
&lt;/additional&gt;
&lt;/product&gt;

<strong>another example feed</strong>

Code:
&lt;products&gt;
&lt;product&gt;
&lt;productID&gt;1344&lt;/productID&gt;
&lt;name&gt;Displex screen&lt;/name&gt;
&lt;price currency="EUR"&gt;6.95&lt;/price&gt;
&lt;productURL&gt;http://www.voorbeeld.nl&lt;/productURL&gt;
&lt;imageURL&gt;http://www.voorbeeld.nl/images/1543.jpg&lt;/imageURL&gt;
&lt;description&gt;&lt;![CDATA[Touchscreen voor je smartphone?]]&gt;&lt;/description&gt;
&lt;categories&gt;
&lt;/categories&gt;
&lt;additional&gt;
&lt;field name="price_from"&gt;14.95&lt;/field&gt;
&lt;field name="price_shipping"&gt;Gratis!&lt;/field&gt;
&lt;field name="soldout"&gt;0&lt;/field&gt;
&lt;field name="startdate"&gt;2012-02-07 12:00:00&lt;/field&gt;
&lt;field name="enddate"&gt;2012-02-08 12:00:00&lt;/field&gt;
&lt;/additional&gt;
&lt;/product&gt;
&lt;/products&gt;

The different feeds (products) i present within a loop like this:

Code:
&lt;?php
    // get xml file contents
    $xmlurl = get_post_meta( get_the_ID( ), 'plus_xmlfeed', true );
    if ( !empty( $xmlurl ) ){
    $xml = simplexml_load_file($xmlurl);
    }
    $xml-&gt;product-&gt;name = substr($xml-&gt;product-&gt;name, 0, 30).'...';
    $desc = substr($xml-&gt;product-&gt;name, 0, 50).'';
?&gt;

    &lt;li class="span3"&gt;
      &lt;div class="thumbnail"&gt;
      &lt;a href="&lt;?php echo $xml-&gt;product-&gt;productURL ;?&gt;"&gt;
          &lt;center&gt;&lt;img src="&lt;?php echo get_template_directory_uri(); ?&gt;/img/logos/&lt;?php echo get_post_meta(get_the_ID( ), 'plus_logos', true); ?&gt;.gif"/&gt;&lt;/center&gt;
          &lt;center&gt;&lt;img class="mainimg" alt="" src="&lt;?php echo $xml-&gt;product-&gt;imageURL ;?&gt;"/&gt;&lt;/center&gt;
        &lt;/a&gt;
        &lt;div class="caption"&gt;
          &lt;h5&gt;&lt;a href="&lt;?php echo $xml-&gt;product-&gt;productURL ;?&gt;"&gt;&lt;?php echo $xml-&gt;product-&gt;name; ?&gt;&lt;/a&gt;&lt;/h5&gt;
          &lt;div class="maindesc half clearfix"&gt;&lt;div class="prijs"&gt;&amp;euro; &lt;?php echo $xml-&gt;product-&gt;price ;?&gt;&lt;/div&gt;&lt;strike style="color: rgb(248, 148, 6);"&gt;
            &lt;?php
            foreach ($xml-&gt;product-&gt;additional-&gt;field as $field) {
                               switch((string) $field['name']) {
                                   case 'price_advice':
                                      echo '&amp;euro; ' .$field. '';
                                      break;
                                   case 'fromPrice':
                                      echo '&amp;euro; ' .$field. '';
                                      break;
                                   case 'oldPrice':
                                      echo '&amp;euro; ' .$field. '';
                                      break;
                                   case 'from_price':
                                      echo '&amp;euro; ' .$field. '';
                                      break;
                                   case 'adviesprijs':
                                      echo '&amp;euro; ' .$field. '';
                                      break;
                                   case 'advice_price':
                                      echo '&amp;euro; ' .$field. '';
                                      break;
                                   case 'Fromprice':
                                      echo '&amp;euro; ' .$field. '';
                                      break;
                                   case 'recommendedPrice':
                                      echo '&amp;euro; ' .$field. '';
                                      break;
                                    }
                                }
            ?&gt;
            &lt;/strike&gt;
          &lt;p style="color: rgb(153, 153, 153);"&gt;&lt;?php echo $desc; ?&gt;&lt;/p&gt;&lt;/div&gt;
          &lt;div class="btn-group bot"&gt;&lt;div class="pull-right"&gt; &lt;/div&gt;&lt;a class="btn btn-primary" href="&lt;?php echo $xml-&gt;product-&gt;productURL ;?&gt;"&gt;Kopen&lt;/a&gt; &lt;a class="btn" href="#&lt;?php echo 'modal_'.get_the_ID();?&gt;"&gt;Info&lt;/a&gt;&lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;

    &lt;/li&gt;
&lt;?php /*end loop*/ ?&gt;

I'm using wordpress to enter the feeds in posts, and display it as a variable ($xmlurl). I would like to enter the specific attributes (<strong>fromPrice</strong> or <strong>from_price</strong> etc) aswell in a post so it does not have to loop trough all option to find the right one for that feed.

I know the above can be a lot faster and more efficient, i hope someone can help me with it. I read something about xpath, and i tried it but without succes.