使用SimpleXML将XML解析为数组

时间:2018-03-29 16:26:23

标签: php xml xml-parsing simplexml-load-string

我从设备

检索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不包含属性:/

我也尝试用递归函数解析它,因为孩子也可能包含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);

这会返回这样的数组

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
                )

        )

)

这是对的吗?

再次感谢Nigel

1 个答案:

答案 0 :(得分:1)

使用SimpleXML加载时,使用print_r()只能了解内容并且没有完整内容。如果您想查看完整内容,请使用->asXML() ...

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

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

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

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