PHP XML - 子节点检索数据
PHP XML - Child nodes retriving data
我在 php 中遇到问题。我只能取回第一个标签的值
这是 XML 文件
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<reservation id="000020" internalId="7366776" >
<ccData ccExpireDate="null/null" />
<reservationDetail id="2_0" currency="" />
<dayPrice detailId="" roomId="2" day="2015-08-01" />
<dayPrice detailId="" roomId="2" day="2015-08-02" />
</reservation>
<reservation id="000010" internalId="7366642" >
<ccData ccCode="Michan" ccNumber="3000000000000" ccExpireDate="12/2016" />
<reservationDetail id="2_0" currency="USD" checkin="2015-07-01" />
<supplement detailId="" roomId="2" description="breakfast" />
<supplement detailId="" roomId="2" description="wifi" />
<supplement detailId="" roomId="2" description="transfer" />
<dayPrice detailId="" roomId="2" day="2015-06-01" />
<dayPrice detailId="" roomId="2" day="2015-06-02" />
<guest detailId="" roomId="2" firstName="Rolando" />
<guest detailId="" roomId="2" firstName="Jessica " />
</reservation>
<reservation id="0005" internalId="7243828" >
<ccData ccCode="453" ccNumber="34983483649365936539" ccExpireDate="05/2016" />
<reservationDetail id="2_0" currency="USD" checkin="2015-05-28" />
<dayPrice detailId="" roomId="2" day="2015-05-29" />
<dayPrice detailId="" roomId="2" day="2015-05-29" />
</reservation>
</Response>
这是 php 检索数据的代码
//SUPPLEMENT
$Cont=0;
echo "SUP >>>>> ".count($xml->reservation[$x]->supplement)."<br>";
do {
foreach($xml->reservation[$x]->supplement->attributes() as $a => $b) {
$Texto='[SUPPLEMENT '.sprintf('%02d', $Cont).'] '.$a.'="'.utf8_decode($b).'"';
echo $Texto."<br>";
}
$Cont++;
} while ($Cont > 99);
这个代码输出是这个
[SUPPLEMENT 00] detailId=""
[SUPPLEMENT 00] roomId="2"
[SUPPLEMENT 00] description="breakfast"
而不是这个
[SUPPLEMENT 00] detailId=""
[SUPPLEMENT 00] roomId="2"
[SUPPLEMENT 01] description="breakfast"
[SUPPLEMENT 01] detailId=""
[SUPPLEMENT 01] roomId="2"
[SUPPLEMENT 01] description="wifi"
[SUPPLEMENT 02] detailId=""
[SUPPLEMENT 02] roomId="2"
[SUPPLEMENT 02] description="transfer"
标签 dayPrice 和 guest 也是同样的问题。
有人可以帮我吗?
提前致谢!!
您的 foreach
循环仅遍历单个 supplement
的属性。您需要另一个循环遍历每个 supplement
和下一个循环遍历每个属性:
do {
foreach($xml->reservation[$x]->supplement as $supplement) {
foreach($supplement->attributes() as $a => $b) {
$Texto='[SUPPLEMENT '.sprintf('%02d', $Cont).'] '.$a.'="'.utf8_decode($b).'"';
echo $Texto."<br>";
}
}
$Cont++;
} while ($Cont > 99);
注意:$x
在你的情况下是1
。
我在 php 中遇到问题。我只能取回第一个标签的值
这是 XML 文件
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<reservation id="000020" internalId="7366776" >
<ccData ccExpireDate="null/null" />
<reservationDetail id="2_0" currency="" />
<dayPrice detailId="" roomId="2" day="2015-08-01" />
<dayPrice detailId="" roomId="2" day="2015-08-02" />
</reservation>
<reservation id="000010" internalId="7366642" >
<ccData ccCode="Michan" ccNumber="3000000000000" ccExpireDate="12/2016" />
<reservationDetail id="2_0" currency="USD" checkin="2015-07-01" />
<supplement detailId="" roomId="2" description="breakfast" />
<supplement detailId="" roomId="2" description="wifi" />
<supplement detailId="" roomId="2" description="transfer" />
<dayPrice detailId="" roomId="2" day="2015-06-01" />
<dayPrice detailId="" roomId="2" day="2015-06-02" />
<guest detailId="" roomId="2" firstName="Rolando" />
<guest detailId="" roomId="2" firstName="Jessica " />
</reservation>
<reservation id="0005" internalId="7243828" >
<ccData ccCode="453" ccNumber="34983483649365936539" ccExpireDate="05/2016" />
<reservationDetail id="2_0" currency="USD" checkin="2015-05-28" />
<dayPrice detailId="" roomId="2" day="2015-05-29" />
<dayPrice detailId="" roomId="2" day="2015-05-29" />
</reservation>
</Response>
这是 php 检索数据的代码
//SUPPLEMENT
$Cont=0;
echo "SUP >>>>> ".count($xml->reservation[$x]->supplement)."<br>";
do {
foreach($xml->reservation[$x]->supplement->attributes() as $a => $b) {
$Texto='[SUPPLEMENT '.sprintf('%02d', $Cont).'] '.$a.'="'.utf8_decode($b).'"';
echo $Texto."<br>";
}
$Cont++;
} while ($Cont > 99);
这个代码输出是这个
[SUPPLEMENT 00] detailId=""
[SUPPLEMENT 00] roomId="2"
[SUPPLEMENT 00] description="breakfast"
而不是这个
[SUPPLEMENT 00] detailId=""
[SUPPLEMENT 00] roomId="2"
[SUPPLEMENT 01] description="breakfast"
[SUPPLEMENT 01] detailId=""
[SUPPLEMENT 01] roomId="2"
[SUPPLEMENT 01] description="wifi"
[SUPPLEMENT 02] detailId=""
[SUPPLEMENT 02] roomId="2"
[SUPPLEMENT 02] description="transfer"
标签 dayPrice 和 guest 也是同样的问题。
有人可以帮我吗?
提前致谢!!
您的 foreach
循环仅遍历单个 supplement
的属性。您需要另一个循环遍历每个 supplement
和下一个循环遍历每个属性:
do {
foreach($xml->reservation[$x]->supplement as $supplement) {
foreach($supplement->attributes() as $a => $b) {
$Texto='[SUPPLEMENT '.sprintf('%02d', $Cont).'] '.$a.'="'.utf8_decode($b).'"';
echo $Texto."<br>";
}
}
$Cont++;
} while ($Cont > 99);
注意:$x
在你的情况下是1
。