在PHP的SimpleXMLElement中访问@attributes数据 [英] Access @attributes data in SimpleXMLElement in PHP

查看:329
本文介绍了在PHP的SimpleXMLElement中访问@attributes数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想先说我已经阅读了本网站上有关此确切问题的很多问题,但我仍在努力将其应用于我的情况.如果有人可以帮助我,那就太好了! :)

Just wanted to start by saying I have read a LOT of the questions on this site about this exact problem, but I'm still struggling to apply it to my scenario. If someone could help me out, that'd be great! :)

我正在尝试从以下XML中提取数据:

I am trying to extract data from the following XML:

$myXML = '<?xml version="1.0" encoding="UTF-8"?>
<products><product uri="https://192.168.110.201:9630/api/products/1807/" id="1807"    resource_type="current"><code>DEMO - MC700X/A</code><flags><inventoried>true</inventoried><editable_sell>false</editable_sell><master_model>false</master_model></flags><sell_price>0.00</sell_price><description>Apple MC700X/A Demo</description><inventory><available>7</available><reserved>0</reserved><coming_for_stock>2.0</coming_for_stock><coming_for_customer>0.0</coming_for_customer><warehouses>0</warehouses><total>7</total></inventory><product_photos/></product></products>';

我正在使用SimpleXML将其放入PHP变量(对象?)中,如下所示:

I am using SimpleXML to put it into a PHP variable (object?) like this:

$xml = new SimpleXMLElement($myXML);

如果我这样做:

echo '<pre>';
print_r($xml);
echo '</pre>';

我得到以下信息:

SimpleXMLElement Object
(
    [product] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [uri] => https://192.168.110.201:9630/api/products/1807/
                    [id] => 1807
                    [resource_type] => current
                )

            [code] => DEMO - MC700X/A
            [flags] => SimpleXMLElement Object
                (
                    [inventoried] => true
                    [editable_sell] => false
                    [master_model] => false
                )

            [sell_price] => 0.00
            [description] => Apple MC700X/A Demo
            [inventory] => SimpleXMLElement Object
                (
                    [available] => 7
                    [reserved] => 0
                    [coming_for_stock] => 2.0
                    [coming_for_customer] => 0.0
                    [warehouses] => 0
                    [total] => 7
                )

            [product_photos] => SimpleXMLElement Object
                (
                )

        )

)

现在,当我尝试以编程方式访问该数据时,可以很好地进行以下操作:

Now, when I try to access that data programatically, the following works fine:

// This returns the value as expected
echo '<pre>';
echo($xml->product->code);
echo '<br>';
echo($xml->product->sell_price);
echo '<br>';
echo($xml->product->inventory->available);
echo '<br>';
echo '</pre>';

这将返回:

DEMO - MC700X/A
0.00
7

但是我需要能够访问基本产品"元素中的"id"标签(即@attributes位),但无法解决.我已经阅读了很多书,并认为我应该可以使用attribute()方法,但是我无法准确地解决它.

But I need to be able to access the "id" tag in the base "product" element (i.e. the @attributes bit), but can't work it out. I've been reading a lot, and figured that I should be able to use the attributes() method, but I can't work it out exactly.

尝试不起作用:

echo '<pre>';
echo($xml->attributes());
echo '<br>';
echo '</pre>';

它什么也没返回.有人可以帮帮我吗?我希望能够显示"id"标记.即,我期望的是:

It just returns nothing. Can someone please help me? I want to be able to display the "id" tag.. i.e. what I would expect to be:

echo $xml['product']['@attributes']['id'];

显然也不起作用.

谢谢! 约翰

推荐答案

您尝试过:

echo (string)$xml->product->attributes()->id;

这应该使您可以访问属性.

That should give you access to the attributes.

如果您有一种以上的产品,则可能是

If you have more than 1 product, it may be

echo (string)$xml->product[0]->attributes()->id;

您还可以使用常规数组表示法访问属性,例如:

You can also access the attributes using regular array notation such as:

$xml->product['id']; // instead of $xml->product->attributes()->id

请参见SimpleXML示例和手册中的示例#5 SimpleXMLElement :: attributes()上的页面以获取更多信息.

See Example #5 from the SimpleXML Examples as well as the manual page on SimpleXMLElement::attributes() for more information.

这篇关于在PHP的SimpleXMLElement中访问@attributes数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆