2012-11-09 32 views
0

我有一個XML OBJ像THID:簡單的XML對象導航的問題

SimpleXMLElement Object 
(
    [@attributes] => Array 
     (
      [id] => survey1_question01 
      [num] => 01 
      [text] => 1. What times will I take my 2 pills of Incivek (telaprevir) each day? 
     ) 

    [response] => SimpleXMLElement Object 
     (
      [@attributes] => Array 
       (
        [dose1] => 08:00 AM 
        [dose2] => 12:02 PM 
        [dose3] => 08:00 PM 
       ) 

     ) 

我怎麼能定位到[文字]和[迴應]。問題是$ xmlTxt-> results-> question [1] - > @屬性不起作用。可能是@符號是我如何繞過這個原因?

回答

1

使用attributes()訪問XML attribues

echo $xmlTxt->results->question[1]->attributes()->text;

1

如果要訪問含特殊字符的對象屬性,請使用{}

$xmlTxt->results->question[1]->{'@attributes'} 
+0

由於某種原因,它返回一個像這樣的空對象SimpleXMLElement對象 ( ) –

+0

@Compiler看到更新 – Musa

+0

仍然存在的問題... $ xmlTxt-> results-> question [1] - > {'@ attributes'}返回一個null屬性是一個數組我可以訪問你說的方式,但我無法訪問該對象。 –