这是我的阵列:
Array (
[Payments] => Array (
[0] => Array (
[Payment] => Array (
[Invoice] => Array (
[InvoiceNumber] => INV-0084
)
[Account] => Array (
[Code] => 260
)
[Date] => 1969-12-31T17:00:00
[Amount] => 119
)
)
[1] => Array (
[Payment] => Array (
[Invoice] => Array (
[InvoiceNumber] => INV-0085
)
[Account] => Array (
[Code] => 260
)
[Date] => 1969-12-31T17:00:00
[Amount] => 132
)
)
)
)
我需要将其转换为XML数据。我使用CakePHP库来转换XML数据。
<Payments>
<Payment>
<Invoice>
<InvoiceNumber>INV-0084</InvoiceNumber>
</Invoice>
<Account>
<Code>260</Code>
</Account>
<Date>2016-06-01T17:00:00 </Date>
<Amount>119</Amount>
</Payment>
<Payment>
<Invoice>
<InvoiceNumber>INV-0085</InvoiceNumber>
</Invoice>
<Account>
<Code>260</Code>
</Account>
<Date>2016-06-01T17:00:00 </Date>
<Amount>132</Amount>
</Payment>
我曾使用过这个功能
$paymentXml = Xml::fromArray($paymentXmlData, array('format' =>'tags'));
$paymentXml = $paymentXml->asXML();
我得到的错误是:
SimpleXMLElement :: __ construct():实体:第3行:解析器错误:文档末尾的额外内容&#34;,&#34;文件&#34;:&#34; / var / www / html / limeactuarial / LIB /蛋糕/效用/ Xml.php&#34;&#34;线&#34;:197
如何使用默认库解决这些问题?
答案 0 :(得分:1)
查看文档。您的数据结构不正确;节点需要嵌套在一个密钥下,该密钥用作节点的名称。
您在那里做的将有效地创建多个根节点,例如:
<Payments>
<Payment>
<Invoice>
<InvoiceNumber>INV-0084</InvoiceNumber>
</Invoice>
...
</Payment>
</Payments>
<Payments>
<Payment>
<Invoice>
<InvoiceNumber>INV-0085</InvoiceNumber>
</Invoice>
...
</Payment>
</Payments>
哪个是无效的XML。您的数据需要像这样构建,其中Payments
将是根节点,包含两个Payment
节点。
array(
'Payments' => array(
'Payment' => array(
array(
'Invoice' => array(
'InvoiceNumber' => 'INV-0084'
),
'Account' => array(
'Code' => '260'
),
'Date' => '1969-12-31T17:00:00',
'Amount' => '119'
),
array(
'Invoice' => array(
'InvoiceNumber' => 'INV-0085'
),
'Account' => array(
'Code' => '260'
),
'Date' => '1969-12-31T17:00:00',
'Amount' => '132'
)
)
)
)
另见: