PHP SimpleXML解析未获取所有属性

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

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

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <packagingOrderResult>
  3. <!-- parameters -->
  4. <par name="documentDate">2022-05-31</par>
  5. <par name="timestamp">2022-05-31 07:05:36</par>
  6. <par name="packagingLineNumber">01</par>
  7. <par name="ppo">000009374196_01</par>
  8. <par name="lotNumber">PW40069</par>
  9. <par name="matid">FP3572</par>
  10. <par name="expirationDate">2022-10-31</par>
  11. <par name="productionDate">2020-11-16</par>
  12. <par name="companyPrefix">50562411</par>
  13. <!-- objects -->
  14. <objects>
  15. <!--default object level is 'boxLevel' (B)-->
  16. <!--default object state is 'OK' (1)-->
  17. <!--state 0 = 'Undefined'-->
  18. <!--state 1 = 'OK'-->
  19. <!--state 2 = 'NotOK'-->
  20. <!--state 3 = 'Sample'-->
  21. <obj idType="Sgtin" id="150127279064515DGB61WXAWADFX" level="B" status="2" />
  22. <obj idType="Sgtin" id="15012727906451RFY1W610481BZ2" level="B" status="2" />
  23. <obj idType="Sgtin" id="15012727906451V0Z2KMP9V91EAH" level="B" status="2" />
  24. </objects>
  25. </packagingOrderResult>

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

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

结果是

  1. SimpleXMLElement Object
  2. (
  3. [comment] => Array
  4. (
  5. [0] => SimpleXMLElement Object
  6. (
  7. )
  8. [1] => SimpleXMLElement Object
  9. (
  10. )
  11. [2] => SimpleXMLElement Object
  12. (
  13. )
  14. )
  15. [par] => Array
  16. (
  17. [0] => 2022-05-31
  18. [1] => 2022-05-31 07:05:36
  19. [2] => 01
  20. [3] => 000009374196_01
  21. [4] => PW40069
  22. [5] => FP3572
  23. [6] => 2022-10-31
  24. [7] => 2020-11-16
  25. [8] => 50562411
  26. )
  27. [objects] => SimpleXMLElement Object
  28. (
  29. [comment] => Array
  30. (
  31. [0] => SimpleXMLElement Object
  32. (
  33. )
  34. [1] => SimpleXMLElement Object
  35. (
  36. )
  37. [2] => SimpleXMLElement Object
  38. (
  39. )
  40. [3] => SimpleXMLElement Object
  41. (
  42. )
  43. [4] => SimpleXMLElement Object
  44. (
  45. )
  46. [5] => SimpleXMLElement Object
  47. (
  48. )
  49. )
  50. [obj] => Array
  51. (
  52. [0] => SimpleXMLElement Object
  53. (
  54. [@attributes] => Array
  55. (
  56. [idType] => Sgtin
  57. [id] => 150127279064515DGB61WXAWADFX
  58. [level] => B
  59. [status] => 2
  60. )
  61. )
  62. [1] => SimpleXMLElement Object
  63. (
  64. [@attributes] => Array
  65. (
  66. [idType] => Sgtin
  67. [id] => 15012727906451RFY1W610481BZ2
  68. [level] => B
  69. [status] => 2
  70. )
  71. )
  72. [2] => SimpleXMLElement Object
  73. (
  74. [@attributes] => Array
  75. (
  76. [idType] => Sgtin
  77. [id] => 15012727906451V0Z2KMP9V91EAH
  78. [level] => B
  79. [status] => 2
  80. )
  81. )
  82. )
  83. )
  84. )

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

vptzau2j

vptzau2j1#

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

  1. $packagingOrderResult = new SimpleXMLElement(getXMLString());
  2. // loop over children with a specific element name
  3. foreach ($packagingOrderResult->par as $par) {
  4. var_dump(
  5. [
  6. // access attribute using array access syntax
  7. 'name' => (string)$par['name'],
  8. // cast to string to read text node child
  9. 'content' => (string)$par
  10. ]
  11. );
  12. }
  13. function getXMLString() {
  14. return <<<'XML'
  15. <?xml version="1.0" encoding="utf-8"?>
  16. <packagingOrderResult>
  17. <!-- parameters -->
  18. <par name="documentDate">2022-05-31</par>
  19. <par name="timestamp">2022-05-31 07:05:36</par>
  20. <par name="packagingLineNumber">01</par>
  21. <par name="ppo">000009374196_01</par>
  22. <par name="lotNumber">PW40069</par>
  23. <par name="matid">FP3572</par>
  24. <par name="expirationDate">2022-10-31</par>
  25. <par name="productionDate">2020-11-16</par>
  26. <par name="companyPrefix">50562411</par>
  27. <!-- objects -->
  28. <objects>
  29. <!--default object level is 'boxLevel' (B)-->
  30. <!--default object state is 'OK' (1)-->
  31. <!--state 0 = 'Undefined'-->
  32. <!--state 1 = 'OK'-->
  33. <!--state 2 = 'NotOK'-->
  34. <!--state 3 = 'Sample'-->
  35. <obj idType="Sgtin" id="150127279064515DGB61WXAWADFX" level="B" status="2" />
  36. <obj idType="Sgtin" id="15012727906451RFY1W610481BZ2" level="B" status="2" />
  37. <obj idType="Sgtin" id="15012727906451V0Z2KMP9V91EAH" level="B" status="2" />
  38. </objects>
  39. </packagingOrderResult>
  40. XML;
  41. }
展开查看全部

相关问题