这令我很沮丧!它看起来很简单,但我不能在我的生活中循环使用这个XML。我从这个链接(http://www.nfl.com/liveupdate/scorestrip/ss.xml)获得了一个SimpleXMLElement对象。我正在尝试向下钻取到g数组并获得[0],[1],[2]等的属性。我只是在学习PHP。所以任何帮助都会受到赞赏。
我试过的代码:
$nfl = simplexml_load_file('http://www.nfl.com/liveupdate/scorestrip/ss.xml');
foreach ($nfl as $game) {
print '<pre>';
print_r($game);
print '</pre>';
}
输出:
SimpleXMLElement Object
(
[@attributes] => Array
(
[w] => 2
[y] => 2014
[t] => R
[gd] => 1
[bph] => 103
)
[g] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[eid] => 2014091100
[gsis] => 56186
[d] => Thu
[t] => 8:25
[q] => F
[h] => BAL
[hnn] => ravens
[hs] => 26
[v] => PIT
[vnn] => steelers
[vs] => 6
[rz] => 0
[ga] =>
[gt] => REG
)
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[eid] => 2014091400
[gsis] => 56187
[d] => Sun
[t] => 1:00
[q] => F
[h] => BUF
[hnn] => bills
[hs] => 29
[v] => MIA
[vnn] => dolphins
[vs] => 10
[rz] => 0
[ga] =>
[gt] => REG
)
)
[2] => SimpleXMLElement Object
(
[@attributes] => Array
(
[eid] => 2014091401
[gsis] => 56188
[d] => Sun
[t] => 1:00
[q] => F
[h] => CAR
[hnn] => panthers
[hs] => 24
[v] => DET
[vnn] => lions
[vs] => 7
[rz] => 0
[ga] =>
[gt] => REG
)
)
[3] => SimpleXMLElement Object
(
[@attributes] => Array
(
[eid] => 2014091402
[gsis] => 56189
[d] => Sun
[t] => 1:00
[q] => F
[h] => CIN
[hnn] => bengals
[hs] => 24
[v] => ATL
[vnn] => falcons
[vs] => 10
[rz] => 0
[ga] =>
[gt] => REG
)
)
[4] => SimpleXMLElement Object
(
[@attributes] => Array
(
[eid] => 2014091403
[gsis] => 56190
[d] => Sun
[t] => 1:00
[q] => F
[h] => CLE
[hnn] => browns
[hs] => 26
[v] => NO
[vnn] => saints
[vs] => 24
[rz] => 0
[ga] =>
[gt] => REG
)
)
[5] => SimpleXMLElement Object
(
[@attributes] => Array
(
[eid] => 2014091404
[gsis] => 56191
[d] => Sun
[t] => 1:00
[q] => F
[h] => MIN
[hnn] => vikings
[hs] => 7
[v] => NE
[vnn] => patriots
[vs] => 30
[rz] => 0
[ga] =>
[gt] => REG
)
)
[6] => SimpleXMLElement Object
(
[@attributes] => Array
(
[eid] => 2014091405
[gsis] => 56192
[d] => Sun
[t] => 1:00
[q] => F
[h] => NYG
[hnn] => giants
[hs] => 14
[v] => ARI
[vnn] => cardinals
[vs] => 25
[rz] => 0
[ga] =>
[gt] => REG
)
)
[7] => SimpleXMLElement Object
(
[@attributes] => Array
(
[eid] => 2014091406
[gsis] => 56193
[d] => Sun
[t] => 1:00
[q] => F
[h] => TEN
[hnn] => titans
[hs] => 10
[v] => DAL
[vnn] => cowboys
[vs] => 26
[rz] => 0
[ga] =>
[gt] => REG
)
)
[8] => SimpleXMLElement Object
(
[@attributes] => Array
(
[eid] => 2014091407
[gsis] => 56194
[d] => Sun
[t] => 1:00
[q] => F
[h] => WAS
[hnn] => redskins
[hs] => 41
[v] => JAC
[vnn] => jaguars
[vs] => 10
[rz] => 0
[ga] =>
[gt] => REG
)
)
[9] => SimpleXMLElement Object
(
[@attributes] => Array
(
[eid] => 2014091408
[gsis] => 56195
[d] => Sun
[t] => 4:05
[q] => F
[h] => SD
[hnn] => chargers
[hs] => 30
[v] => SEA
[vnn] => seahawks
[vs] => 21
[rz] => 0
[ga] =>
[gt] => REG
)
)
[10] => SimpleXMLElement Object
(
[@attributes] => Array
(
[eid] => 2014091409
[gsis] => 56196
[d] => Sun
[t] => 4:05
[q] => F
[h] => TB
[hnn] => buccaneers
[hs] => 17
[v] => STL
[vnn] => rams
[vs] => 19
[rz] => 0
[ga] =>
[gt] => REG
)
)
[11] => SimpleXMLElement Object
(
[@attributes] => Array
(
[eid] => 2014091410
[gsis] => 56197
[d] => Sun
[t] => 4:25
[q] => F
[h] => DEN
[hnn] => broncos
[hs] => 24
[v] => KC
[vnn] => chiefs
[vs] => 17
[rz] => 0
[ga] =>
[gt] => REG
)
)
[12] => SimpleXMLElement Object
(
[@attributes] => Array
(
[eid] => 2014091411
[gsis] => 56198
[d] => Sun
[t] => 4:25
[q] => F
[h] => GB
[hnn] => packers
[hs] => 31
[v] => NYJ
[vnn] => jets
[vs] => 24
[rz] => 0
[ga] =>
[gt] => REG
)
)
[13] => SimpleXMLElement Object
(
[@attributes] => Array
(
[eid] => 2014091412
[gsis] => 56199
[d] => Sun
[t] => 4:25
[q] => F
[h] => OAK
[hnn] => raiders
[hs] => 14
[v] => HOU
[vnn] => texans
[vs] => 30
[rz] => 0
[ga] =>
[gt] => REG
)
)
[14] => SimpleXMLElement Object
(
[@attributes] => Array
(
[eid] => 2014091413
[gsis] => 56200
[d] => Sun
[t] => 8:30
[q] => F
[h] => SF
[hnn] => 49ers
[hs] => 20
[v] => CHI
[vnn] => bears
[vs] => 28
[rz] => 0
[ga] =>
[gt] => REG
)
)
[15] => SimpleXMLElement Object
(
[@attributes] => Array
(
[eid] => 2014091500
[gsis] => 56201
[d] => Mon
[t] => 8:30
[q] => P
[h] => IND
[hnn] => colts
[hs] => 0
[v] => PHI
[vnn] => eagles
[vs] => 0
[rz] => 0
[ga] =>
[gt] => REG
)
)
)
)
我尝试了这个foreach,但它没有输出任何内容:
foreach ($nfl->g as $game) {
print '<pre>';
print_r($game);
print '</pre>';
}
答案 0 :(得分:1)
$nfl->g
不存在,您还需要获取属性而不是内容,因为它们都是空白的。您应该获得以下成功。请注意XML $nfl->gms->g
中的正确深度。另请注意使用attributes()
电话。
$nfl = simplexml_load_file('http://www.nfl.com/liveupdate/scorestrip/ss.xml');
print '<pre>';
foreach ($nfl->gms->g as $game) {
foreach($game->attributes() as $a => $b) {
echo $a,'="',$b,"\"\n";
}
echo "\n------------------------------------------\n";
}
print '</pre>';