Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
126 views
in Technique[技术] by (71.8m points)

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

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:

one example feed

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

another example feed

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

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

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

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

    </li>
<?php /*end loop*/ ?>

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 (fromPrice or from_price 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

My own solution:

i found the solution myself

            <?php
                $FromPrices= get_post_meta(get_the_ID( ), 'plus_fromprices', true);
                if ( !empty( $FromPrices) ){
                foreach ($xml->xpath('//field[@name="'.$FromPrices.'"]') as $fromprice) {
                    echo '&euro; '.$fromprice;
                    }
                }
            ?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...