SimpleXMLElement:检索命名空间属性
我已经阅读了数百篇相关的 SO 条目,但我无法让它发挥作用。我真的看不出我做错了什么。我肯定在做一些明显愚蠢的事情,但目前我看不到它。
这就是我的我正在做:
$shopUrl = "http://api.spreadshirt.net/api/v1/shops/614852/productTypes?".
"locale=de_DE&fullData=false&limit=20&offset=0"
$ch = curl_init($shopUrl);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$result = curl_exec($ch);
curl_close($ch);
$products = new SimpleXMLElement($result);
foreach ($products->productType as $product) {
$resources = $product->children('http://www.w3.org/1999/xlink');
$resEntity = array(
'id' => (int)$product->attributes()->id,
'name' => (string)$product->name[0],
'price' => (string)$product->price[0]->vatIncluded[0],
'preview' => $resources
);
echo '<pre>'.print_r($resEntity, true).'</pre>';
}
这输出了我,
Array
(
[id] => 6
[name] => Männer T-Shirt klassisch
[price] => 9.90
[preview] => SimpleXMLElement Object
(
[@attributes] => Array
(
[href] => http://api.spreadshirt.net/api/v1/shops/614852/productTypes/6
)
)
)
我现在正在尝试访问 HREF 属性,但到目前为止我尝试过的一切$resources->attributes()->href
或 $resources['href']
但 PHP 一直说 Node 不再存在
。
I've read about hundreds of SO-entries about that, but I can't get it to work. I can't really see what I'm doing wrong. I'm most certainly doing something obviously stupid, but at the moment I can't see it.
I'm trying to parse http://api.spreadshirt.net/api/v1/shops/614852/productTypes?locale=de_DE&fullData=false&limit=20&offset=0
This is what I'm doing:
$shopUrl = "http://api.spreadshirt.net/api/v1/shops/614852/productTypes?".
"locale=de_DE&fullData=false&limit=20&offset=0"
$ch = curl_init($shopUrl);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$result = curl_exec($ch);
curl_close($ch);
$products = new SimpleXMLElement($result);
foreach ($products->productType as $product) {
$resources = $product->children('http://www.w3.org/1999/xlink');
$resEntity = array(
'id' => (int)$product->attributes()->id,
'name' => (string)$product->name[0],
'price' => (string)$product->price[0]->vatIncluded[0],
'preview' => $resources
);
echo '<pre>'.print_r($resEntity, true).'</pre>';
}
This outputs me
Array
(
[id] => 6
[name] => Männer T-Shirt klassisch
[price] => 9.90
[preview] => SimpleXMLElement Object
(
[@attributes] => Array
(
[href] => http://api.spreadshirt.net/api/v1/shops/614852/productTypes/6
)
)
)
I'm now trying to access the HREF-attribute but everything I've tried so far like $resources->attributes()->href
or $resources['href']
but PHP keeps on saying Node no longer exists
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须在
attributes()
方法中指定命名空间。我想(attributes()<的手册中没有详细解释/code>
)您必须使用第一个参数指定 xml 命名空间。这可能会从
xlink
命名空间获取href
属性。否则,您只需从 default xml 命名空间获取属性,即type
和mediaType
(或从哪个节点获取属性)。它应该像这样工作(未经测试):
You must specify the namespace in the
attributes()
method. I guess (it's not explained in detail in the manual ofattributes()
) you have to specify the xml namespace with the first argument. This might get you thehref
attribute from thexlink
namespace. Otherwise you just get the attributes from the default xml namespace, namelytype
andmediaType
(or from which node you fetch the attributes).It should be work like this (not tested):