2012-03-24 2 views
1

나는이 같은 XML 파일 하바 :XML 파일에서 루프를 반복 하시겠습니까?

<name> 
    <entry> 
     <date>2012-03-18 13:53:23</date> 
     <ID>1</ID> 
     <category>questions</category> 
     <question>who are you?</question> 
    <answers> 
     <answer text='a man' id='1'/> 
     <answer text='a woman' id='2'/> 
     <answer text='an animal' id='3'/> 
     <answer text='an alien' id='4'/> 
    </answers> 
     <author>Gorge</author> 
    </entry> 
</name> 

을 그리고 난 루프에 모든 필드를하려고 해요,하지만 난 대답 지점에 도착하면 그것은 루프 모든 해답 dosent

어떤 조언을 내가 어떻게 할 수있는 그걸 관리해?

내가 사용하고 있습니다 :

foreach($xml->entry as $entry){ 
echo "<p>"; 
echo "<strong>Author:</strong> ".$entry->author."<br/>"; 
echo "</p>"; 
} 

을 결과를 얻기 위해. 당신이 SimpleXML을를 사용하는 것처럼 사전에

고맙습니다 패트릭

+0

가 어떻게 필드를 루핑? 코드? XSLT 사용? –

+0

코드 어딘가에'foreach ($ entry-> answers-> answer as $ answer)'가 있습니까? – Tomalak

답변

1

보인다. 당신은 아마이 라인을 따라 뭔가를하고 싶지 :

foreach($xml->entry as $entry){ 
    //iterating through your entry-elements 
    echo $entry->question . '<br />'; 

    foreach($entry->answers->answer as $answer) { 
    //iterating through the anwers of the entry-element 
    echo $answer['text'] . '<br />'; 
    } 
} 

출력 : 당신입니다

?

  • 사람
  • 동물
  • 여자
  • 당신은있는 DOMDocument() 클래스를 사용하여이 작업을 수행 할 수 있습니다

http://codepad.org/Nfbo4l59

0

외계인입니다.

<?php 
    $xml="<name> 
     <entry> 
      <date>2012-03-18 13:53:23</date> 
      <ID>1</ID> 
      <category>questions</category> 
      <question>who are you?</question> 
     <answers> 
      <answer text='a man' id='1'/> 
      <answer text='a woman' id='2'/> 
      <answer text='an animal' id='3'/> 
      <answer text='an alien' id='4'/> 
     </answers> 
      <author>Gorge</author> 
     </entry> 
    </name> 
    "; 
    $dom=new DOMDocument(); 
    $dom->loadXML($xml); 

    foreach($dom->getElementsByTagName('entry') as $tagentry) 
    { 
     foreach($tagentry->getElementsByTagName('answers') as $taganswers) 
     { 
      foreach($taganswers->getElementsByTagName('answer') as $taganswer) 
      { 
       $taganswer_array[]=$taganswer->getAttribute('text'); 
      } 
     } 
    } 

    echo "<pre>"; 
    print_r($taganswer_array); 
    echo "</pre>"; 
    ?> 

출력 ::

Array(

    [0] => a man 

    [1] => a woman 

    [2] => an animal 

    [3] => an alien 

)

관련 문제