SimpleXMLElement:检索命名空间属性

发布于 2024-11-05 19:16:27 字数 1780 浏览 2 评论 0原文

我已经阅读了数百篇相关的 SO 条目,但我无法让它发挥作用。我真的看不出我做错了什么。我肯定在做一些明显愚蠢的事情,但目前我看不到它。

我正在尝试解析 http://api.spreadshirt.net/api/v1/shops/614852/productTypes?locale=de_DE&fullData=false&limit=20&offset=0

这就是我的我正在做:

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红尘作伴 2024-11-12 19:16:27

您必须在 attributes() 方法中指定命名空间。我想(attributes()<的手册中没有详细解释/code>)您必须使用第一个参数指定 xml 命名空间。这可能会从 xlink 命名空间获取 href 属性。否则,您只需从 default xml 命名空间获取属性,即 typemediaType(或从哪个节点获取属性)。

它应该像这样工作(未经测试):

$resources = $product->resources[0];   // <resources> node
$resource = $resources->resource[0];   // first <resource> node
$attribs = $resource->attributes('http://www.w3.org/1999/xlink');   // fetch all attributes from the given namespace
var_dump($attribs->href);   // or maybe var_dump((string)$attribs->href);

You must specify the namespace in the attributes() method. I guess (it's not explained in detail in the manual of attributes()) you have to specify the xml namespace with the first argument. This might get you the href attribute from the xlink namespace. Otherwise you just get the attributes from the default xml namespace, namely type and mediaType (or from which node you fetch the attributes).

It should be work like this (not tested):

$resources = $product->resources[0];   // <resources> node
$resource = $resources->resource[0];   // first <resource> node
$attribs = $resource->attributes('http://www.w3.org/1999/xlink');   // fetch all attributes from the given namespace
var_dump($attribs->href);   // or maybe var_dump((string)$attribs->href);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文