【问题标题】:Get field from XML file with PHP使用 PHP 从 XML 文件中获取字段
【发布时间】:2017-12-22 15:21:01
【问题描述】:

我正在尝试从 PHP 中的 XML 文件中提取一些字段。这适用于大多数领域,但其中一些给我错误,我不明白为什么。

XML 示例:

<products>
  <product>
    <ns2:name>Some name</ns2:name>
    <offers>
      <offer id="123">
        <ns2:productUrl>https://www.something.here</ns2:productUrl>
        <priceHistory>
          <ns2:price currency="EUR">299.00</ns2:price>
        </priceHistory>
      </offer>
    </offers>
  </product>
</products>

我已经有了名字:

$name = mysql_real_escape_string($products->product->children('ns2', true)->name);

我想对 price 和 ProductUrl 做同样的事情,但这不起作用:

$URL = $products->product->offers->offer->children('ns2',true)->productUrl;    
$price = $products->product->offers->offer->priceHistory->children('ns2', true)->price;

谁能告诉我为什么这不起作用以及如何获得产品网址和价格?

【问题讨论】:

  • “那行不通”是什么意思?这些行应该返回正确的元素,请参阅eval.in/924453
  • 第一个错误是注意:试图获取非对象的属性。第二个错误是致命错误:在 null 上调用成员函数 children()。两者都在获取 $URL 的线上。在第二个错误之后,脚本没有继续前进。

标签: php xml children


【解决方案1】:

想通了。似乎顶层有两个孩子。第一个没有任何数据。这就是导致错误的原因。收到 null 并想得到 null 的孩子,这显然不起作用。

【讨论】:

    猜你喜欢
    • 2010-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-09
    • 1970-01-01
    • 2011-01-30
    • 2018-08-02
    相关资源
    最近更新 更多