在 PHP 中访问 SimpleXMLElement 中的 @attributes 数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7523044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Access @attributes data in SimpleXMLElement in PHP
提问by John Cleary
Just wanted to start by saying I haveread 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! :)
只是想首先说我已经阅读了这个网站上关于这个确切问题的很多问题,但我仍然在努力将它应用到我的场景中。如果有人可以帮助我,那就太好了!:)
I am trying to extract data from the following XML:
我正在尝试从以下 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>';
I am using SimpleXML to put it into a PHP variable (object?) like this:
我正在使用 SimpleXML 将它放入一个 PHP 变量(对象?),如下所示:
$xml = new SimpleXMLElement($myXML);
If I do a:
如果我做:
echo '<pre>';
print_r($xml);
echo '</pre>';
I get the following returned:
我得到以下返回:
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>';
This returns:
这将返回:
DEMO - MC700X/A
0.00
7
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.
但我需要能够访问基本“产品”元素(即@attributes 位)中的“id”标签,但无法解决。我读了很多书,并认为我应该能够使用 attributes() 方法,但我无法完全解决。
Trying this didn't work:
尝试这个没有用:
echo '<pre>';
echo($xml->attributes());
echo '<br>';
echo '</pre>';
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:
它只是什么都不返回。有人可以帮帮我吗?我希望能够显示“id”标签..即我期望的是:
echo $xml['product']['@attributes']['id'];
obviously doesn't work either.
显然也不起作用。
Thanks!! John
谢谢!!约翰
回答by drew010
Did you try:
你试过了吗:
echo (string)$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
如果您有超过 1 个产品,则可能是
echo (string)$xml->product[0]->attributes()->id;
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
See Example #5from the SimpleXML Examples as well as the manual page on SimpleXMLElement::attributes()for more information.
有关更多信息,请参阅SimpleXML 示例中的示例 #5以及SimpleXMLElement::attributes()上的手册页。
回答by u7021946
$array = (array)$obj;
$prop_id = $array['@attributes'];