PHP SimpleXML解析未获取所有属性

xuo3flqw  于 2023-06-20  发布在  PHP
关注(0)|答案(1)|浏览(98)

我试图解析一个XML文件,这是由制造设备产生的。XML文件结构如下所示:

<?xml version="1.0" encoding="utf-8"?>
<packagingOrderResult>
  <!-- parameters -->
  <par name="documentDate">2022-05-31</par>
  <par name="timestamp">2022-05-31 07:05:36</par>
  <par name="packagingLineNumber">01</par>
  <par name="ppo">000009374196_01</par>
  <par name="lotNumber">PW40069</par>
  <par name="matid">FP3572</par>
  <par name="expirationDate">2022-10-31</par>
  <par name="productionDate">2020-11-16</par>
  <par name="companyPrefix">50562411</par>
<!-- objects -->
  <objects>
    <!--default object level is 'boxLevel' (B)-->
    <!--default object state is 'OK' (1)-->
    <!--state 0 = 'Undefined'-->
    <!--state 1 = 'OK'-->
    <!--state 2 = 'NotOK'-->
    <!--state 3 = 'Sample'-->
    <obj idType="Sgtin" id="150127279064515DGB61WXAWADFX" level="B" status="2" />
    <obj idType="Sgtin" id="15012727906451RFY1W610481BZ2" level="B" status="2" />
    <obj idType="Sgtin" id="15012727906451V0Z2KMP9V91EAH" level="B" status="2" />
  </objects>
</packagingOrderResult>

我可以像这样将文件读入数组:

$parsed=simplexml_load_file($path.$file);
print_r($parsed);

结果是

SimpleXMLElement Object
(
    [comment] => Array
        (
            [0] => SimpleXMLElement Object
                (
                )

            [1] => SimpleXMLElement Object
                (
                )

            [2] => SimpleXMLElement Object
                (
                )

        )

    [par] => Array
        (
            [0] => 2022-05-31
            [1] => 2022-05-31 07:05:36
            [2] => 01
            [3] => 000009374196_01
            [4] => PW40069
            [5] => FP3572
            [6] => 2022-10-31
            [7] => 2020-11-16
            [8] => 50562411
        )

[objects] => SimpleXMLElement Object
        (
            [comment] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                        )

                    [1] => SimpleXMLElement Object
                        (
                        )

                    [2] => SimpleXMLElement Object
                        (
                        )

                    [3] => SimpleXMLElement Object
                        (
                        )

                    [4] => SimpleXMLElement Object
                        (
                        )

                    [5] => SimpleXMLElement Object
                        (
                        )

                )

            [obj] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [idType] => Sgtin
                                    [id] => 150127279064515DGB61WXAWADFX
                                    [level] => B
                                    [status] => 2
                                )

                        )

                    [1] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [idType] => Sgtin
                                    [id] => 15012727906451RFY1W610481BZ2
                                    [level] => B
                                    [status] => 2
                                )

                        )

                    [2] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [idType] => Sgtin
                                    [id] => 15012727906451V0Z2KMP9V91EAH
                                    [level] => B
                                    [status] => 2
                                )

                        )
                )
        )
)

正如您所看到的,后面的 objects 部分被很好地解析,所有属性都被关联到数组中(idType、id、level、status)。然而,第一个部分,par,被解析成一个简单的数字索引数组,忽略了 name 属性。
请记住,这是我第一次处理和解析XML文件,所以很多工作都是猜测。有没有办法把文件的第一部分放到一个很好的关联数组中,就像对象的后半部分一样?

vptzau2j

vptzau2j1#

这只是一个调试输出,不包括所有数据。这就是为什么json_encode($simpleXMLElement)可能不包括更复杂的XML的所有数据。
数据还在这里。SimpleXML如何处理元素取决于您如何访问它。

$packagingOrderResult = new SimpleXMLElement(getXMLString());

// loop over children with a specific element name
foreach ($packagingOrderResult->par as $par) {
    var_dump(
        [
            // access attribute using array access syntax
            'name' => (string)$par['name'],
            // cast to string to read text node child 
            'content' => (string)$par
        ]
    );
}

function getXMLString() {
  return <<<'XML'
<?xml version="1.0" encoding="utf-8"?>
<packagingOrderResult>
  <!-- parameters -->
  <par name="documentDate">2022-05-31</par>
  <par name="timestamp">2022-05-31 07:05:36</par>
  <par name="packagingLineNumber">01</par>
  <par name="ppo">000009374196_01</par>
  <par name="lotNumber">PW40069</par>
  <par name="matid">FP3572</par>
  <par name="expirationDate">2022-10-31</par>
  <par name="productionDate">2020-11-16</par>
  <par name="companyPrefix">50562411</par>
<!-- objects -->
  <objects>
    <!--default object level is 'boxLevel' (B)-->
    <!--default object state is 'OK' (1)-->
    <!--state 0 = 'Undefined'-->
    <!--state 1 = 'OK'-->
    <!--state 2 = 'NotOK'-->
    <!--state 3 = 'Sample'-->
    <obj idType="Sgtin" id="150127279064515DGB61WXAWADFX" level="B" status="2" />
    <obj idType="Sgtin" id="15012727906451RFY1W610481BZ2" level="B" status="2" />
    <obj idType="Sgtin" id="15012727906451V0Z2KMP9V91EAH" level="B" status="2" />
  </objects>
</packagingOrderResult>
XML;
}

相关问题