2011-09-23 2 views
24

그냥 이 정확한 문제에 대해이 사이트에서 많은 질문을 읽었지만 지금도 내 시나리오에 적용하는 데 어려움을 겪고 있습니다. 누군가 나를 도울 수 있다면, 좋을 것입니다! :) 나는 다음과 같은 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']; 

분명히 작동하지 않습니다.

감사합니다. 존

+0

가능한 중복 (http://stackoverflow.com/questions/1652128/accessing-attribute-from-simplexml) – hakre

답변

53

당신은 시도해 봤어 : 주어야한다

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

당신은 속성에 액세스 할 수 있습니다.

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

가 SimpleXML을 예에서 Example #5를 참조하십시오 : 당신이 1 개 이상의 제품이있는 경우

, 그것은 또한 같은 규칙적인 배열 표기법을 사용하여 속성에 액세스 할 수 있습니다

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

될 수있다 자세한 내용은 SimpleXMLElement::attributes()의 매뉴얼 페이지를 참조하십시오.

+0

드류, 당신이 사람이야! 정말 고마워.나는 그것을 시도했지만, 결과를 문자열로 캐스트해야만 추측 할 수있는 것처럼 보입니다. :) –

+0

예, 성가신 것처럼 보이며 잊어 버릴 수도 있습니다. 캐스트없이 액세스하면 여전히 가치가 있지만 SimpleXMLElement이므로 특정 인스턴스에서 사용하려고하면 올바르게 작동하지 않습니다. – drew010

+2

또한 [ '@attributes']를 사용하기 때문에 SimpleXMLElement를 먼저 배열로 캐스팅해야합니다 – Enrique

0
[SimpleXML을에서 @attribute 액세스]의
$array = (array)$obj; 
$prop_id = $array['@attributes']; 
관련 문제