0

With the help of Stackoverflow users I got quite a few steps of my problem. I have this XML witch is one of the child from the root node:

<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>

and using this php code

 $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>" ;
   }

I can get

DMAT - F1= F2= F3=

FERRO - F1= F2= F3=

but I'm stuck in how to get the values of F1, F3 and F3

What I need as result is:

DMAT - F1=62 F2=34 F3=8

FERRO - F1=2 F2=5 F3=10

So that I can save all of them in a Mysql database

Im new with XML and PHP so I guess this should be a easy one.

Any help appreciated!

Community
  • 1
  • 1
Mestre1966
  • 33
  • 6
  • possible duplicate of [Getting actual value from PHP SimpleXML node](http://stackoverflow.com/questions/1133931/getting-actual-value-from-php-simplexml-node) – Daniel Apr 27 '14 at 15:29

1 Answers1

0

you need to access the value of $defect instead of the attribute ClassID of $channel in line 3 of your code:

foreach($channel->DefectCounter as $defect){
    echo $defect->attributes()->ClassId, "= ";
    echo $channel['ClassId'],"  " ; // <----- change needed here
} 

In line 3, you want to access the value of the node, not the attribute:

echo $defect;
michi
  • 6,565
  • 4
  • 33
  • 56
  • @Mestre1966 Welcome, I recommend http://www.php.net/manual/en/simplexml.examples-basic.php – michi Apr 27 '14 at 16:13