使用来自多个xmlfeeds的php获取基于属性值的XML元素

时间:2012-02-08 18:33:00

标签: php xml xpath

首先,我有一个包含多个外部xml供稿(目前有24种不同供稿)的多个产品的首页,每个供稿1个产品。某些属性对于同一事物具有不同的名称,例如:

一个示例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>

另一个示例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>

我在循环中出现的不同Feed(产品)如下:

<?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*/ ?>

我正在使用wordpress在帖子中输入Feed,并将其显示为变量($ xmlurl)。我想在帖子中输入特定属性( fromPrice from_price 等),以便它不必循环通过所有选项来找到该Feed的正确属性

我知道上面的内容可以更快,更有效,我希望有人可以帮助我。我读了一些关于xpath的东西,我尝试了但没有成功。

1 个答案:

答案 0 :(得分:0)

我自己的解决方案:

我自己找到了解决方案

            <?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;
                    }
                }
            ?>