简体   繁体   中英

Convert PHP nested array to XML tree structure

I have following array in PHP and I want to convert it equivalent XML.

$data = array("VitalParameters"=>array(
                    "Details"=>array(
                        "PID"=>1234,
                        "OPID"=>1345
                    ),
                    "Parameters"=>array(
                        "HR"=>112,
                        "SPO2"=>0
                )));

Following XML tree structure which I expected

<VitalParameters>
    <Details>
      <PID>1234</PID>
      <OPID>1345</OPID>
    </Details>
    <Parameters>
      <HR>112</HR>
      <SPO2>0</SPO2>
    </Parameters>
</VitalParameters>

I tried many things but no luck. If any more information required just comment I will give additional information.

Try Array2XML ( http://www.lalit.org/lab/convert-php-array-to-xml-with-attributes ) this has worked for me.

$data = array(
  "Details" => array(
    "PID" => 1234,
    "OPID" => 1345
  ),
  "Parameters" => array(
    "HR" => 112,
    "SPO2" => 0
));


$xml = Array2XML::createXML('VitalParameters', $data)->saveXML();

echo $xml;

Output

<?xml version="1.0" encoding="UTF-8"?>
<VitalParameters>
  <Details>
    <PID>1234</PID>
    <OPID>1345</OPID>
  </Details>
  <Parameters>
    <HR>112</HR>
    <SPO2>0</SPO2>
  </Parameters>
</VitalParameters>

This should get you on the right track, probably needs some tuning though (especially in regards to keys aswell as checking if $arr is an array/object in the first place... this was just to show you how to recursively iterate them and print the data).

Also, this snippet does not support XML attributes in any way.

<?php
$data = array("VitalParameters"=>array(
                    "Details"=>array(
                        "PID"=>1234,
                        "OPID"=>1345
                    ),
                    "Parameters"=>array(
                        "HR"=>112,
                        "SPO2"=>0
                )));

function ArrayToXML($arr, $depth = 0, $maxDepth = 2)
{
    $retVal = "";
    foreach($arr as $key => $value)
    {
        $retVal .= "<" . $key . ">";

        $type = gettype($value);
        switch($type)
        {
            case "array":
            case "object":
                if ($depth < $maxDepth)
                    $retVal .= ArrayToXml($value, $depth+1, $maxDepth);
                else
                    $retVal .= $type; // Depth >= MaxDepth, not iterating here
                break;
            case "boolean":
                $retVal .= $value ? "true" : "false";
                break;
            case "resource":
                $retVal .= "Resource";
            case "NULL":
                $retVal .= "NULL";
            default:
                $retVal .= $value;
        }
        $retVal .= "</" . $key . ">\n";
    }
    return $retVal;
}

echo ArrayToXML($data);
?>

Output:

<VitalParameters><Details><PID>1234</PID>
<OPID>1345</OPID>
</Details>
<Parameters><HR>112</HR>
<SPO2>0</SPO2>
</Parameters>
</VitalParameters>

I got solution like following code

<?php
header("Content-type: text/xml; charset=utf-8");
$data = array(
    "Details" => array(
        "PID" => "1234","OPID" => "1345"
    ),"Parameters" => array(
        "HR" => "112","SPO2" => "0"
    )
);

$domtree = new DOMDocument('1.0', 'UTF-8');
/* create the root element of the xml tree */
$xmlRoot = $domtree->createElement("VitalParameters");
/* append it to the document created */
$xmlRoot = $domtree->appendChild($xmlRoot);

foreach ($data as $key=>$value)
{
    $currentElement= $domtree->createElement($key);
    $currentElement= $xmlRoot->appendChild($currentElement);
    if(is_array($value))
    {
        foreach ($value as $k=>$v)
        {
            $currentElement->appendChild($domtree->createElement($k,$v));
        }
    }
}

/* get the xml printed */
echo $domtree->saveXML();

Output

<VitalParameters>
  <Details>
    <PID>1234</PID>
    <OPID>1345</OPID>
  </Details>
  <Parameters>
    <HR>112</HR>
    <SPO2>0</SPO2>
  </Parameters>
</VitalParameters>

I think - this is must be more simple:

function arrayToXML($arr){

  $s = "";

  foreach($arr as $k => $v){
    if(is_array($v)){$v = arrayToXML($v);}
    $s .= "<{$k}>{$v}</{$k}>";
  }

  return $s;}

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