2017-05-10 4 views
0

누군가가 나를 도와 줄 수있는 특정 문제가 있습니다. 마침내 여기까지 오랫동안 트롤을 걸어 왔지만, 내가 원하는 것을하기위한보다 효율적인 방법이 있다고 느낍니다.필드 배열로 속성별로 XML 데이터 가져 오기

<api:relationship> 
    <api:related> 
     <api:object> 
      <api:records> 
       <api:record> 
        <api:native> 
         <api:field name="publisher"> 
          <api:text>Some Guy</api:text> 
         </api:field> 
         <api:field name="title"> 
          <api:text>This is the title</api:text> 
         </api:field> 
         <api:field name="volume"> 
          <api:text>33</api:text> 
         </api:field> 
        </api:native> 
       </api:record> 
      </api:records> 
     </api:object> 
    </api:related> 
</api:relationship> 

그리고이 방법이 모든 정보를 얻고있다 : 나는 그 느낌

$xml=simplexml_load_string($data); 
$xml->registerXPathNamespace('api', 'http://www.something.com/api'); 

foreach($xml->xpath('//api:relationship') as $header) 
{ 
    foreach($header->children('api', true)->related->object as $items) { 
     foreach ($items->records->record->native->field as $fields) { 

      if ((string) $fields->attributes()->name == 'title') { 
       echo $fields->text; 
      } 
     } 
    } 

우스꽝 방법입니다 어쨌든 .... 다음은 XML 파일의 간단한 버전은 내가 한 것입니다 그것을하는 것.

여기에는 여러 개의 레코드가 있으며 각 필드를 통해 정보를 얻습니다.

누군가가 그 정보에 직접 액세스 할 수있는 방법을 보여주기를 바랍니다.

내가 좋아하는 일을 시도하고있다 :

$items['title']->text; 

$items->attributes()->title->text; 

foreach($items->attributes() as $a => $b) { 
    echo $a,'="',$b,"\"\n"; 
} 

하지만 마지막은 의미가 난 단지 바로 얻을 수있는 하나 개의 값이 주어진하지 않습니다?

도움 주셔서 감사합니다.

답변

1

당신은이 같은 특정 속성을 특정 값 찾기 위해 XPath를 사용할 수 있습니다

$title = $xml->xpath('//api:field[@name="title"]');