I have following XML:
<?xml version="1.0" encoding="Windows-1250"?>
<rsp:responsePack version="2.0" id="Za001" state="ok" note="" programVersion="10600.125 E1 (22.1.2014)"
xmlns:rsp="http://www.stormware.cz/schema/version_2/response.xsd"
xmlns:lst="http://www.stormware.cz/schema/version_2/list.xsd"
xmlns:lStk="http://www.stormware.cz/schema/version_2/list_stock.xsd"
xmlns:stk="http://www.stormware.cz/schema/version_2/stock.xsd">
<rsp:responsePackItem version="2.0" id="a55" state="ok">
<lStk:listStock version="2.0" dateTimeStamp="2014-04-18T22:36:44" dateValidFrom="2014-04-18" state="ok">
<lStk:stock version="2.0">
<stk:stockHeader>
<stk:id>101</stk:id>
<stk:EAN>8594011770127</stk:EAN>
<stk:isSales>true</stk:isSales>
<stk:name>Item1</stk:name>
<stk:categories>
<stk:idCategory>5</stk:idCategory>
<stk:idCategory>6</stk:idCategory>
</stk:categories>
</stk:stockHeader>
</lStk:stock>
<lStk:stock version="2.0">
<stk:stockHeader>
<stk:id>114</stk:id>
<stk:EAN>8595557507840</stk:EAN>
<stk:name>Item2</stk:name>
<stk:categories>
<stk:idCategory>6</stk:idCategory>
<stk:idCategory>9</stk:idCategory>
<stk:idCategory>1</stk:idCategory>
</stk:categories>
</stk:stockHeader>
</lStk:stock>
</lStk:listStock>
</rsp:responsePackItem>
</rsp:responsePack>
There are two items with the lStk:stock attribute, and foreach of them I would need to get some values.
My current code is:
$xml=simplexml_load_file("import.xml");
if ($xml) {
$ns = $xml->getDocNamespaces();
$data = $xml->children($ns['rsp']);
$items = $data->children($ns['lStk']);
foreach ($items as $item) {
$counter = 0;
$elements = $item->children($ns['lStk'])->children($ns['stk'])->children($ns['stk']);
$products[$counter]['ean'] = $elements->EAN;
$products[$counter]['name'] = $elements->name;
$products[$counter]['count'] = $elements->count;
$i = 0;
foreach ($elements->categories as $category) {
/** @var $ class_name */
foreach ($category as $cat) {
/** @var $cat class_name */
$products[$counter]['category'][$i] = $cat;
$i++;
}
}
}
}
var_dump($products);
Which returns the info only for the first product. How should I change it to get both products?