1

This is my array:

 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
             )
        )
    )
)

I need to convert it to XML data. I have used the CakePHP library to convert the XML data.

<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>

I had used the function

$paymentXml = Xml::fromArray($paymentXmlData, array('format' =>'tags'));
$paymentXml = $paymentXml->asXML();

The error I got was:

SimpleXMLElement::__construct(): Entity: line 3: parser error : Extra content at the end of the document","file":"/var/www/html/limeactuarial/lib/Cake/Utility/Xml.php","line":197

How do I solve these by using the default library?

3
  • 1
    possible duplicate stackoverflow.com/questions/17428323/… Commented Jun 1, 2016 at 13:48
  • I like to have the answer using cakephp library Commented Jun 1, 2016 at 13:50
  • I can easily convert array without [0] and [1] which you can see in array (i.e) single array. Commented Jun 1, 2016 at 13:56

1 Answer 1

2

Check the documentation. Your data is not correctly structured; nodes need to be nested under a single key, which serves as the name for the nodes.

What you are doing there will effectively create multiple root nodes, like:

<Payments>
    <Payment>
        <Invoice>
            <InvoiceNumber>INV-0084</InvoiceNumber>
        </Invoice>
        ...
    </Payment>
</Payments>
<Payments>
    <Payment>
        <Invoice>
            <InvoiceNumber>INV-0085</InvoiceNumber>
        </Invoice>
        ...
    </Payment>
</Payments>

Which is invalid XML. Your data needs to be structured like this, where Payments will be the root node, containing two Payment nodes.

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'
            )
        )
    )
)

See also:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.