Php 循环xml文件中的循环?

Php 循环xml文件中的循环?,php,xml,simplexml,Php,Xml,Simplexml,我有这样一个xml文件: <name> <entry> <date>2012-03-18 13:53:23</date> <ID>1</ID> <category>questions</category> <question>who are you?</question> <answers&g

我有这样一个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>

2012-03-18 13:53:23
1.
问题
你是谁?
峡谷
我试图循环所有字段,但当我到达答案点时,它会循环所有答案

有什么建议可以帮我吗

我正在使用:

foreach($xml->entry as $entry){
echo "<p>";
echo "<strong>Author:</strong> ".$entry->author."<br/>";
echo "</p>";
}
foreach($xml->entry as$entry){
回声“”;
echo“作者:”$entry->Author.“
”; 回声“

”; }
为了得到结果

先发制人
Patrik看起来像是在使用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 />';
  }
}
foreach($xml->entry as$entry){
//遍历条目元素
echo$entry->问题。“
”; foreach($entry->answers->answers as$answer){ //遍历entry元素的anwers echo$answer['text']。
; } }
输出:

你是谁

  • 男人
  • 女人
  • 动物
  • 外星人

您似乎正在使用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 />';
  }
}
foreach($xml->entry as$entry){
//遍历条目元素
echo$entry->问题。“
”; foreach($entry->answers->answers as$answer){ //遍历entry元素的anwers echo$answer['text']。
; } }
输出:

你是谁

  • 男人
  • 女人
  • 动物
  • 外星人

您可以通过使用DOMDocument()类来实现这一点


)

您可以通过使用DOMDocument()类来实现这一点


)

您是如何循环字段的?密码?使用XSLT?在您的代码中是否有
foreach($entry->answers->answer as$answer)
?如何循环字段?密码?使用XSLT?在您的代码中是否有
foreach($entry->answers->answer as$answer)
Array(

   [0] => a man  

   [1] => a woman 

   [2] => an animal 

   [3] => an alien