2012-03-24 7 views
1

私はこのようなxmlファイルHAVA: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>"; 
} 

を結果を得るために。事前 で

ありがとうパトリック

+0

どのようにフィールドをループしていますか?コードですか? XSLTについて –

+0

あなたのコードのどこかに 'foreach($ entry-> answers-> answer as $ answer)'がありますか? – Tomalak

答えて

1

あなたはSimpleXMLをを使用しているように思えます。おそらく、これらの線に沿って何かをしたい:

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 

関連する問題