简体   繁体   中英

Parse XML to Array with SimpleXML

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

When loading with SimpleXML, using print_r() gives only an idea of the content and doesn't have the full content. If you want to see the full content then use ->asXML() ...

$array = simplexml_load_string($xml);
echo $array->asXML();

To check the attributes of <ptu> element, (this just gives the first one)...

echo $array->ptu[0]['name'];

This uses ->ptu to get the <ptu> element and takes the first one [0] and then takes the name attribute using ['name'] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM