简体   繁体   English

当XML有变量时,PHP解析XML问题

[英]PHP Parse XML issues when XML has variables

Pls see snippet of XML file below: 请参阅下面的XML文件片段:

<products>
<product>
<id>2589527</id>
<name>Samsung PS42C450</name>
<manufacturer>Samsung</manufacturer>
<manufacturer-id>22</manufacturer-id>
<description>42 in, Widescreen, Plasma, HD Ready, Samsung DNIe+ ,</description>
<category>Sound and Vision &gt; Vision &gt; TVs</category>
<category-id>2</category-id>
<number-of-retailers>16</number-of-retailers>
<image-url height="217" width="300">http://images.pricerunner.com/product/300x217/101294172/Samsung-PS42C450.jpg</image-url>
<rating type="average">
  <average>4,1</average>
</rating>
<rating type="professional">
  <average>4,1</average>
  <num-ratings>1</num-ratings>
</rating>
<lowest-price currency="GBP">354.44</lowest-price>
<highest-price currency="GBP">549.00</highest-price>
</product>
</products>

I'm having problems parsing image-url, lowest-price and highest-price 我在解析图片网址,最低价格和最高价格方面遇到了问题

I'm trying: 我正在努力:

$lowprice = $products->lowest-price;

$highprice = $products->highest-price;

$imageURL = $products->image-url;

But they are returning nothing - any ideas what I'm doing wrong? 但他们什么都没有回来 - 任何想法我做错了什么?

use $products->{'lowest-price'}. 使用$ products - > {'最低价'}。 (with curly braces you can use special characters such as the minus sign) (使用花括号,你可以使用特殊字符,如减号)

Shai. 夏嘉曦。

$products->lowest-price;

PHP will interpret as PHP将解释为

($products->lowest)  minus price;

There's no "lowest" in your $products object, and price is almost certainly not a defined constant, so both show up as null, get cast to 0's, and end up producing 0-0=0 你的$ products对象中没有“最低”,而且price几乎肯定不是定义的常量,所以两者都显示为null,得到0,然后最终产生0-0=0

It's probably because of the dash (assuming other properties work). 这可能是因为破折号(假设其他属性有效)。 Try encapsulating them with curly braces, like 尝试用大括号封装它们,比如

$products->{highest-price};

There are (maybe) two unrelated issues. 有(可能)两个不相关的问题。

1. XML structure 1. XML结构

Your (broken) code, eg $products->lowest-price is wanting to access the lowest-price element which is a child of $products . 您的(破损)代码,例如$products->lowest-price想要访问lowest-price元素,这是$products的子元素。 Assuming the variable is named after the element, you have a little more work to do. 假设变量以元素命名,您还有一些工作要做。 The XML is structured like XML的结构类似于

<products>
 └─ <product>
     └─ <lowest-price>

So if $products is the `products> element, then three steps are needed. 因此,如果$products是`products>元素,则需要三个步骤。

$products->product->lowest-price

The above may be a non-issue, and simply unfortunate variable naming. 以上可能是一个非问题,简直不幸的变量命名。

2. Dash (-) in element name 2.元素名称中的短划线( - )

When presented with $a->bc , PHP sees that as $a->b <minus> c as bc is not a valid label (and no a valid object property name). 当提供$a->bc ,PHP将其视为$a->b <minus> c因为bc不是有效标签(并且没有有效的对象属性名称)。 As noted on the "SimpleXML Basic usage" manual page ( link ), the appropriate solution is to use the variable property syntax ala. 正如“SimpleXML Basic用法”手册页( 链接 )中所述,适当的解决方案是使用变量属性语法ala。

$products->product->{'lowest-price'}

See: http://php.net/simplexml.examples-basic (Example #3) 请参阅: http//php.net/simplexml.examples-basic (示例#3)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM