0

我只想在 PHP foreach 循环中显示以下 XML 记录,

<?xml version="1.0" encoding="utf-8"?>

<Node>
  <game_takes>
    <game_take>
      <id type="System.String">0</id>
      <name type="System.String">take1</name>
    </game_take>
    <game_take>
      <id type="System.String">1</id>
      <name type="System.String">take2</name>
    </game_take>
    <game_take>
      <id type="System.String">2</id>
      <name type="System.String">take3</name>
  </game_take>
  <game_take>
    <id type="System.String">3</id>
    <name type="System.String">take4</name>
  </game_take>
  </game_takes>
</Node>

我一直在尝试的是

$grabme = simplexml_load_file('gamecast.xml');

  foreach ($grabme->game_take as $key) {

  echo $key['name'];

  }

似乎没有什么工作,有人可以帮帮我吗?

4

2 回答 2

1

你需要继续链接foreach

$xml = simplexml_load_file("xml.xml");
foreach ($xml->game_takes->game_take as $game_take) {
    $game_take_id_attributes = $game_take->id->attributes();
    // remember string typecast
    echo (string) $game_take->id ."\n";
    echo (string) $game_take_id_attributes->type ."\n";
    // and so on ...
}
于 2013-01-24T19:46:32.550 回答
0

simplexml 加载 XML,就好像它是一个数组一样,使用 $obj 将 xml 对象保存为代表根节点。尝试

foreach($grabme->game_takes as $take) {
                          ^----note the S
   echo $take->name

反而。

于 2013-01-24T19:02:05.140 回答