xml - PHP SimpleXML Children Node Data Extract -
i have been trying extract node data's children using foreach loop. can main node data without problems, child data gets first data set repeats. can maybe tell me doing wrong? got example going offical documentation: http://php.net/manual/en/simplexmlelement.children.php
this 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 php code
$file = file_get_contents('play.xml', true); $xmltosave = new simplexmlelement($file); foreach ($xmltosave->result_data->root_xml->stock $element) { echo $element->stock_code . "\n"; //child node data - barcode foreach ($xmltosave->result_data->root_xml->stock->multiple_barcodes $element) { echo $element->barcode . "\n"; } }
this output:
28
barcode1
123
barcode1
expected output:
28
barcode1
barcode2
123
barcode123
barcode124
in inner loop use foreach ($element->multiple_barcodes->barcode $barcode) { echo $barcode; }
.
Comments
Post a Comment