doubeng9407 2012-05-24 08:26
浏览 88
已采纳

在Foreach循环中的XPath结果上运行XPath

I'm working on a project that reads datafeeds like these:

<?xml version="1.0" encoding="UTF-8"?>
<producten>
  <product>
    <sku><![CDATA[75100192B]]></sku>
    <price><![CDATA[349.00]]></price>
    <old_price><![CDATA[499.00]]></old_price>
    <in_stock><![CDATA[1]]></in_stock>
    <delivery_time><![CDATA[1 - 2 werkdagen]]></delivery_time>
    <manufacturer><![CDATA[Kids River]]></manufacturer>
    <name><![CDATA[Kids River Urban Stroller 3 in 1 Black / Grey  Black Frame]]></name>
    <description><![CDATA[KidsRiver Urban Stroller]]></description>
    <url><![CDATA[http://www.url.com/]]></url>
    <categorie><![CDATA[Kinderwagens]]></categorie>
    <subcategorie><![CDATA[Kinderwagen]]></subcategorie>
    <model><![CDATA[Kids River Urban]]></model>
    <collectiejaar><![CDATA[2011]]></collectiejaar>
    <shipping_price><![CDATA[0.00]]></shipping_price>
    <image><![CDATA[http://www.url.com/image.jpg]]></image>
  </product>
  <product>
    <sku><![CDATA[Joolz Day Silver / Black - 2012]]></sku>
    <price><![CDATA[849.00]]></price>
    <old_price><![CDATA[879.00]]></old_price>
    <in_stock><![CDATA[1]]></in_stock>
    <delivery_time><![CDATA[1 - 2 werkdagen]]></delivery_time>
    <manufacturer><![CDATA[Joolz]]></manufacturer>
    <name><![CDATA[Joolz Day Silver / Black]]></name>
    <description><![CDATA[Joolz Day Compleet.]]></description>
    <url><![CDATA[http://www.url.com/]]></url>
    <categorie><![CDATA[Kinderwagens]]></categorie>
    <subcategorie><![CDATA[Kinderwagen]]></subcategorie>
    <model><![CDATA[Joolz Day]]></model>
    <collectiejaar><![CDATA[2012]]></collectiejaar>
    <shipping_price><![CDATA[0.00]]></shipping_price>
    <image><![CDATA[http://www.url.com/image.jpg]]></image>
  </product>
</producten>

What I want to do it to make a loop that can read individual products from this feed so I made something like this:

// Spin up the SimpleXML parser
$xml_feed = new SimpleXMLElement($feed_data);

$products = $xml_feed->xpath('//producten/product');

foreach ($products as $product)
{
    $name = $product->xpath('./name');
    $price = $product->xpath('price');
    $old_price = $product->xpath('old_price');
    $in_stock = $product->xpath('in_stock');
    $manufacturer = $product->xpath('manufacturer');

    echo print_r( $name);
    echo var_dump((string) $price[0]);
    echo var_dump((string) $old_price[0]);
    echo var_dump((string) $in_stock[0]);
    echo var_dump((string) $manufacturer[0]);
    echo "
";
}

I was wondering if there is a better way to read a xml when using XPath 2 times. One before the loop and the other XPath within the loop. I'm doing it this way because I want to be able to parse all kinds of feeds.

I'm a little concerned about the [0] behind the field: $price[0].

  • 写回答

1条回答 默认 最新

  • dongzhiyi2006 2012-05-24 08:32
    关注

    "I was wondering if there is a better way to read a xml when using XPath 2 times. One before the loop and the other XPath within the loop."

    Why? I can see nothing wrong with that.

    "I'm a little concerned about the [0] behind the field: $price[0]."

    xpath() always returns a node list, even if there is only one node to select. The [0] is correct.


    Anyway, since you're using SimpleXML, you could write:

    $producten = new SimpleXMLElement($feed_data);
    
    foreach ($producten->product as $product) 
    {
      echo $product->price . "
    ";
      echo $product->old_price . "
    ";
      echo $product->in_stock . "
    ";
      echo $product->manufacturer . "
    ";
    }
    

    http://codepad.org/SDDPBFhn

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试