그냥 이이 정확한 문제에 대해이 사이트에서 많은 질문을 읽었지만 지금도 내 시나리오에 적용하는 데 어려움을 겪고 있습니다. 누군가 나를 도울 수 있다면, 좋을 것입니다! :) 나는 다음과 같은 XML에서 데이터를 추출하기 위해 노력하고PHP에서 SimpleXMLElement의 @attributes 데이터에 액세스
:
$xml = new SimpleXMLElement($myXML);
경우 : 나는 (? 오브젝트)이 같은 PHP 변수로 넣어 SimpleXML을 사용하고
$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>';
을
echo '<pre>';
print_r($xml);
echo '</pre>';
내가 반환 된 다음 얻을 :
01 내가 할 이제 23,516,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
(
)
)
)
, 내가 프로그래밍 방식으로 데이터를 액세스 할 때, 다음과 같은 잘 작동 :
// 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 비트)하지만 작동하지는 않습니다. 나는 많은 것을 읽었으며, attributes() 메서드를 사용할 수 있어야한다고 생각했지만, 정확하게 처리 할 수는 없다.
이 작동하지 않았다 시도 :
echo '<pre>';
echo($xml->attributes());
echo '<br>';
echo '</pre>';
그냥 아무 것도 반환하지 않습니다. 누군가 나를 도울 수 있습니까? 'id'태그를 표시하고 싶습니다. 즉, 내가 무엇을 기대할 수 있습니까? 즉,
echo $xml['product']['@attributes']['id'];
분명히 작동하지 않습니다.
감사합니다. 존
가능한 중복 (http://stackoverflow.com/questions/1652128/accessing-attribute-from-simplexml) – hakre