0

I am creating xml file from array. I found the link How to convert array to SimpleXML and tried creating xml using ans provided by user Hanmant.

Input array

$data = array(
  'Pieces' => array(
    'Piece' => array(
      array(
        'PieceID' => '1',
        'Weight' => '0.5',
      ),
      array(
        'PieceID' => '2',
        'Weight' => '2.0',
      ),
    ),
  ),
);    

But I am getting result as

<Pieces>
  <Piece>
     <item0>
        <PieceID>1</PieceID>
        <Weight>0.5</Weight>
     </item0>
     <item1>
        <PieceID>2</PieceID>
        <Weight>2.0</Weight>
     </item1>
  </Piece>
</Pieces>

How can i get result like

<Pieces>
  <Piece>
     <PieceID>1</PieceID>
     <Weight>0.5</Weight>
  </Piece>
  <Piece>
     <PieceID>2</PieceID>
     <Weight>2.0</Weight>
  </Piece>
</Pieces>
Community
  • 1
  • 1
  • 1
    You should probably look up some other method, because the comments to the answer you linked here indicate that this method doesnot work for the cases such as yours (Nested arrays). – Tarun Gaba Dec 23 '14 at 06:57

3 Answers3

0

Read through all the answers at the link you provided, there are several solutions suggested there that are better than the accepted answer.

For instance one of the answer there referred to this class: http://www.lalit.org/lab/convert-php-array-to-xml-with-attributes/

Not only it allows you to include attributes there, but it also allows you to generate XML like you want.

Community
  • 1
  • 1
obaranovsky
  • 165
  • 7
0

check this site were you may get an answer to your query

http://www.phpro.org/classes/PHP-Recursive-Array-To-XML-With-DOM.html

0

The array structure you have is different to the one for the answer by Hanmant and therefore you've picked the wrong function for the job.

However what you ask for does require only very little code with SimpleXMLElement when you do it with a recursive function:

$data = array(
    'Pieces' => array(
        'Piece' => array(
            array(
                'PieceID' => '1',
                'Weight'  => '0.5',
            ),
            array(
                'PieceID' => '2',
                'Weight'  => '2.0',
            ),
        ),
    ),
);

$xml = create($data);

with the following defintion of create:

function create($from, SimpleXMLelement $parent = null, $tagName = null)
{
    if (!is_array($from)) {
        if ($tagName === null) {
            $parent[0] = (string) $from;
        } else {
            $parent->addChild($tagName, (string) $from);
        }
        return $parent;
    }

    foreach ($from as $key => $value) {
        if (is_string($key)) {
            if ($parent === null) {
                $parent = new SimpleXMLElement("<$key/>");
                create($value, $parent);
                break;
            }
            create($value, $parent, $key);
        } else {
            create($value, $parent->addChild($tagName));
        }
    }

    return $parent;
}

This function first handles string values to set the element's node-values. It then traverses the array which has at least the tagname for a single element or multiple elements. If the document does not yet exists, it is created and the elements children are added to it (recursion). Otherwise just the elements children are added (recursion).

This is example code with little error handling, so take care you follow the format of the array you've outlined in your question.

Output (beautified):

<?xml version="1.0"?>
<Pieces>
  <Piece>
    <PieceID>1</PieceID>
    <Weight>0.5</Weight>
  </Piece>
  <Piece>
    <PieceID>2</PieceID>
    <Weight>2.0</Weight>
  </Piece>
</Pieces>
Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836