使用php从多个xmlfeed中获取基于属性值的XML元素 [英] Get XML element based on attribute value with php from multiple xmlfeeds

查看:25
本文介绍了使用php从多个xmlfeed中获取基于属性值的XML元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我有一个首页,其中包含来自多个外部 xml 提要(目前有 24 个不同提要)的多个产品,每个提要 1 个产品.某些属性对同一事物有不同的名称,例如:

一个示例供稿

<产品><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><描述><![CDATA[包括.顺便提一句!!Verkrijgbaar in een voordeelset.]]></description><类别><category path="Aanbiedingen">Aanbiedingen</category><category path="Projectverlichting">Projectverlichting</category></类别><附加><field name="fromPrice">395.00</field><field name="discount">196.00</field><field name="deliveryCosts"></field><field name="deliveryTime">3-5 dagen</field></附加></产品>

另一个示例供稿

<产品><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 智能手机?]]></description><类别></类别><附加><field name="price_from">14.95</field><field name="price_shipping">免费!</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></附加></产品></产品>

我在这样的循环中呈现的不同提要(产品):

product->name = substr($xml->product->name, 0, 30).'...';$desc = substr($xml->product->name, 0, 50).'';?>
  • <div class="缩略图"><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);"><?phpforeach ($xml->product->additional->field as $field) {switch((string) $field['name']) {案例price_advice":回声'&欧元;' .$ 字段.'';休息;案例'fromPrice':回声'&欧元;' .$ 字段.'';休息;案例旧价格":回声'&欧元;' .$ 字段.'';休息;案例'from_price':回声'&欧元;' .$ 字段.'';休息;案例adviesprijs":回声'&欧元;' .$ 字段.'';休息;案例advice_price":回声'&欧元;' .$ 字段.'';休息;案例'Fromprice':回声'&欧元;' .$ 字段.'';休息;案例推荐价格":回声'&欧元;' .$ 字段.'';休息;}}?></罢工><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();?>">信息</a></div>
  • <?php/*结束循环*/?>

    我正在使用 wordpress 在帖子中输入提要,并将其显示为变量 ($xmlurl).我想在帖子中输入特定属性(fromPricefrom_price 等),这样就不必遍历所有选项来为该提要找到正确的选项.

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

    解决方案

    我自己的解决方案:

    我自己找到了解决方案

     xpath('//field[@name="'.$FromPrices.'"]') as $fromprice) {回声'&欧元;'.$fromprice;}}?>

    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.

    解决方案

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

    这篇关于使用php从多个xmlfeed中获取基于属性值的XML元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    PHP最新文章
    热门教程
    热门工具
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆