I've that xml structure retrieving from device
<packet> <info action="fiscalmemory" fiscalmemorysize="1048576" recordsize="464" fiscal="1" uniqueno="ABC12345678" nip="123-456-78-90" maxrecordscount="2144" recordscount="7" maxreportscount="1830" reportscount="4" resetmaxcount="200" resetcount="0" taxratesprglimit="30" taxratesprg="1" currencychangeprglimit="4" currencychangeprg="0" fiscalstartdate="dd-mm-yyyy hh:dd:ss" fiscalstopdate="dd-mm-yyyy hh:dd:ss" currencyname="PLN" /> <ptu name="A" bres="Nobi">123.23</ptu> <ptu name="B">123.23</ptu> <ptu name="D">8</ptu> <sale>999.23</sale> </packet> simpleXml does't see ptu's attributes
$array = simplexml_load_string($xml); print_r($array); It prints
SimpleXMLElement Object ( [info] => SimpleXMLElement Object ( [@attributes] => Array ( [action] => fiscalmemory [fiscalmemorysize] => 1048576 [recordsize] => 464 [fiscal] => 1 [uniqueno] => ABC12345678 [nip] => 123-456-78-90 [maxrecordscount] => 2144 [recordscount] => 7 [maxreportscount] => 1830 [reportscount] => 4 [resetmaxcount] => 200 [resetcount] => 0 [taxratesprglimit] => 30 [taxratesprg] => 1 [currencychangeprglimit] => 4 [currencychangeprg] => 0 [fiscalstartdate] => dd-mm-yyyy hh:dd:ss [fiscalstopdate] => dd-mm-yyyy hh:dd:ss [currencyname] => PLN ) ) [ptu] => Array ( [0] => 123.23 [1] => 123.23 [2] => 8 ) [sale] => 999.23 ) As we can see ptu doesn't contain attributes :/
I also tried parse it with recursive function because children also may contain chilren but without success :/
Could anybody point to me why SimpleXML doesn't take ptu's attributes and also share any parsing function?
Thanks in advance.
edited
As regards Nigel Ren I made that function
function parseXMLtoArray($xml){ $x = simplexml_load_string($xml); $result = []; function parse($xml, &$res){ $res['name'] = $xml->getName(); $res['value'] = $xml->__toString(); foreach ($xml->attributes() as $k => $v){ $res['attr'][$k] = $v->__toString(); } foreach($xml->children() as $child){ parse($child, $res['children'][]); } } parse($x, $result); return $result; } $resArray = parseXMLtoArray($rawXml); print_r($resArray); this returns such array
Array ( [name] => packet [value] => [attr] => Array ( [crc] => BKJFKHKD54 ) [children] => Array ( [0] => Array ( [name] => info [value] => [attr] => Array ( [action] => fiscalmemory [fiscalmemorysize] => 1048576 [recordsize] => 464 [fiscal] => 1 [uniqueno] => ABC12345678 [nip] => 123-456-78-90 [maxrecordscount] => 2144 [recordscount] => 7 [maxreportscount] => 1830 [reportscount] => 4 [resetmaxcount] => 200 [resetcount] => 0 [taxratesprglimit] => 30 [taxratesprg] => 1 [currencychangeprglimit] => 4 [currencychangeprg] => 0 [fiscalstartdate] => dd-mm-yyyy hh:dd:ss [fiscalstopdate] => dd-mm-yyyy hh:dd:ss [currencyname] => PLN ) ) [1] => Array ( [name] => ptu [value] => 123.23 [attr] => Array ( [name] => A ) ) [2] => Array ( [name] => ptu [value] => 123.23 [attr] => Array ( [name] => B ) ) [3] => Array ( [name] => ptu [value] => 8 [attr] => Array ( [name] => D ) ) [4] => Array ( [name] => sale [value] => 999.23 ) ) ) Is this correct?
Thanks again Nigel