在Stackoverflow用户的帮助下,我遇到了很多问题。 我有这个XML女巫是来自根节点的孩子之一:
<ChannelStatistics ChannelId="DMAT" CounterDim="">
<TotalCount>104</TotalCount>
<DefectCounter ClassId="F1">62</DefectCounter>
<DefectCounter ClassId="F2">34</DefectCounter>
<DefectCounter ClassId="F3">8</DefectCounter>
</ChannelStatistics>
<ChannelStatistics ChannelI="FERRO" CounterDim="">
<TotalCount>17</TotalCount>
<DefectCounter ClassId="F1">2</DefectCounter>
<DefectCounter ClassId="F2">5</DefectCounter>
<DefectCounter ClassId="F3">10</DefectCounter>
</ChannelStatistics>
并使用此PHP代码
$obj = simplexml_load_file('data\data.xml');
foreach($obj->PieceReport->Piece->DeviceValuation->ChannelStatistics as $channel){
echo $channel->attributes()->ChannelId;
echo " - ";
foreach($channel->DefectCounter as $defect){
echo $defect->attributes()->ClassId, "= ";
echo $channel['ClassId']," " ;
}
echo "<br>" ;
}
我可以
DMAT - F1 = F2 = F3 =
FERRO - F1 = F2 = F3 =
但我仍然坚持如何获得F1,F3和F3的值
我需要的是:
DMAT-F1 = 62 F2 = 34 F3 = 8
FERRO - F1 = 2 F2 = 5 F3 = 10
这样我就可以将它们全部保存在Mysql数据库中
我是XML和PHP的新手,所以我想这应该是一个简单的。
任何帮助表示赞赏!
答案 0 :(得分:0)
您需要访问$defect
的值,而不是代码第3行中ClassID
的属性 $channel
:
foreach($channel->DefectCounter as $defect){
echo $defect->attributes()->ClassId, "= ";
echo $channel['ClassId']," " ; // <----- change needed here
}
在第3行中,您想要访问节点的值,而不是属性:
echo $defect;