使用 SimpleXML 将 XML 解析为数组

Parse XML to Array with SimpleXML

我已经从设备

中检索了 xml 结构
<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 看不到 ptu 的属性

$array = simplexml_load_string($xml);
print_r($array);

它打印

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
)

我们可以看到 ptu 不包含属性 :/

我也尝试用递归函数解析它,因为 children 也可能包含 chilren 但没有成功:/

谁能告诉我为什么 SimpleXML 不采用 ptu 的属性并共享任何解析函数?

提前致谢。

已编辑

关于Nigel Ren 我做了那个功能

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);

这个returns这样的数组

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
                )

        )

)

这是正确的吗?

再次感谢奈杰尔

使用 SimpleXML 加载时,使用 print_r() 只能提供内容的概念,并不能提供全部内容。如果您想查看完整内容,请使用 ->asXML()...

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

要检查 <ptu> 元素的属性,(这只给出第一个)...

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

这使用 ->ptu 获取 <ptu> 元素并获取第一个 [0] 然后使用 ['name'].

获取名称属性