PHP-元素中带有点的XML(Laravel)

时间:2018-07-18 13:55:36

标签: php xml laravel

我将这个解析器包用于Laravel: https://github.com/orchestral/parser

我有一个看起来像这样的XML:

<MilestonesFollow-upReportUSERDEFINEDItem>
     <NextMilestoneDescCur.Comp.InclRltd>Description of the stone</NextMilestoneDescCur.Comp.InclRltd>
</MilestonesFollow-upReportUSERDEFINEDItem>

但是,由于元素的名称中包含点.,因此将其视为新元素。这就是我解析的方式:

$data = $xml->parse([
     'milestones' => ['uses' => 'MilestonesFollow-upReportUSERDEFINEDItem[NextMilestoneDescCur.Comp.InclRltd]', 'default' => null]
]);

但是,这给了我下面的数组,该数组由点.分隔:

array:1 [▼
  "milestones" => array:524 [▼
    0 => array:1 [▼
      "NextMilestoneDescCur" => array:1 [▼
        "Comp" => array:1 [▼
          "InclRltd" => null
        ]
      ]
    ]
]

2 个答案:

答案 0 :(得分:0)

不幸的是,Orchestral/Parser不支持此功能:

我向开发人员提出了这个问题: https://github.com/laravie/parser/issues/18#event-1740422636,他写道:

 > Definitely not possible, sorry.

答案 1 :(得分:0)

我创建了一个程序包,可以帮助您将xml解析为数组。

https://github.com/mtownsend5512/xml-to-array

$xml = '<MilestonesFollow-upReportUSERDEFINEDItem><NextMilestoneDescCur.Comp.InclRltd>Description of the stone</NextMilestoneDescCur.Comp.InclRltd></MilestonesFollow-upReportUSERDEFINEDItem>';

dd(xml_to_array($xml));
// Will yield: 

array:1 [▼
  "NextMilestoneDescCur.Comp.InclRltd" => "Description of the stone"
]