0

I hope someone could give me some help here. I Have this XML file witch channelStatistics is one of several children from the main 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>

How do I get to the specific child (ChannelStatistics) and then get the data (ClassId="F1", ClassId="F2", ClassId="F3") for different ChannelId?

I need a result like:

DMAT - F1=62 F2=34 F3=8     
FERRO - F1=2 F2=5 F3=10

How can I do it?

3
  • Are you using SimpleXML or XMLReader? Commented Apr 26, 2014 at 18:22
  • If you tried, then show how you tried in your question: we might be able to see where you went wrong if we can see your code; but have no idea what you're doing wrong if you don't Commented Apr 26, 2014 at 18:35
  • $reader = new XMLReader(); $reader->open("data\data.xml"); while($reader->read()) { if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == 'ChannelStatistics') { $F1 = $reader->getAttribute('ClassId="F1"'); $F2 = $reader->getAttribute('ClassId="F2"'); $F3 = $reader->getAttribute('ClassId="F2"'); } $reader->close(); Commented Apr 26, 2014 at 18:50

1 Answer 1

1

Using SimpleXML:

$obj = simplexml_load_string($str); // or use simplexml_load_file($file)

foreach($obj->ChannelStatistics as $channel){
    echo $channel->attributes()->ChannelId;

    foreach($channel->DefectCounter as $defect){
        echo $defect->attributes()->ClassId;
    }
}

Note: the XML must have a root node, and the ChannelStatistics should be children of the root. Otherwise modify the foreach accordingly. You can also use the syntax $channel['ChannelId'] to get an attirbute.

Sign up to request clarification or add additional context in comments.

3 Comments

I still can't get any result, but have no errors $obj = simplexml_load_file('data\data.xml'); foreach($obj->ChannelStatistics as $channel){ echo $channel->attributes()->ChannelId; foreach($channel->DefectCounter as $defect){ echo $defect->attributes()->ClassId; } }
the XML file ant the following nodes /NdtReport/PieceReport/Piece/DeviceValuation/ChannelStatistics/DefectCounter
ok I got the DMATF1F2F3FerroF1F2F3 , but not the values of F1 , F2 or F3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.