简体   繁体   中英

PHP how to convert array to xml

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>

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.

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

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM