XML : PHP SimpleXML Children Node Data Extract

I have been trying extract the node data's children using a foreach loop. I can get the main node data without problems, but the child data only gets the first data set then repeats. can someone maybe tell me what i am doing wrong? I got the example going from the offical documentation: http://php.net/manual/en/simplexmlelement.children.php

This is my test xml

  <Result>  <Result_Data>      <Root_XML>          <Stock>              <Stock_Code>28</Stock_Code>              <Barcode>28</Barcode>              <Multiple_Barcodes>                  <Barcode>BARCODE1</Barcode>                  <Barcode>BARCODE2</Barcode>              </Multiple_Barcodes>              <Sell_Prices>                  <Sell_Price>                      <Inclusive>10</Inclusive>                      <Exclusive>8.77193</Exclusive>                  </Sell_Price>                  <Sell_Price>                      <Inclusive>0</Inclusive>                      <Exclusive>0</Exclusive>                  </Sell_Price>                  <Sell_Price>                      <Inclusive>0</Inclusive>                      <Exclusive>0</Exclusive>                  </Sell_Price>              </Sell_Prices>              <New_Prices>                  <New_Price>0</New_Price>                  <New_Price>0</New_Price>                  <New_Price>0</New_Price>                  <New_Price>0</New_Price>              </New_Prices>              <Work_In_Progress_Quantity>0</Work_In_Progress_Quantity>              <Extended_Description />              <Onhand>0</Onhand>          </Stock>          <Stock>              <Stock_Code>123</Stock_Code>              <Barcode>123</Barcode>              <Multiple_Barcodes>                  <Barcode>BARCODE123</Barcode>                  <Barcode>BARCODE124</Barcode>              </Multiple_Barcodes>              <Sell_Prices>                  <Sell_Price>                      <Inclusive>10</Inclusive>                      <Exclusive>8.77193</Exclusive>                  </Sell_Price>                  <Sell_Price>                      <Inclusive>0</Inclusive>                      <Exclusive>0</Exclusive>                  </Sell_Price>                  <Sell_Price>                      <Inclusive>0</Inclusive>                      <Exclusive>0</Exclusive>                  </Sell_Price>              </Sell_Prices>              <New_Prices>                  <New_Price>0</New_Price>                  <New_Price>0</New_Price>                  <New_Price>0</New_Price>                  <New_Price>0</New_Price>              </New_Prices>              <Work_In_Progress_Quantity>0</Work_In_Progress_Quantity>              <Extended_Description />              <Onhand>0</Onhand>          </Stock>                  </Root_XML>  </Result_Data>    

This is my php code

  $file = file_get_contents('play.xml', true);    $XMLtoSAVE = new SimpleXMLElement($file);  foreach ($XMLtoSAVE->Result_Data->Root_XML->Stock as $element) {  echo $element->Stock_Code . "\n";        //Child Node Data - Barcode      foreach ($XMLtoSAVE->Result_Data->Root_XML->Stock->Multiple_Barcodes as $element) {      echo $element->Barcode . "\n";      }      }       

This is the output:

28

BARCODE1

123

BARCODE1

Expected Output:

28

BARCODE1

BARCODE2

123

BARCODE123

BARCODE124

No comments:

Post a Comment