将PHP嵌套数组转换为XML树结构

时间:2014-08-20 05:27:03

标签: php xml

我在PHP中有以下数组,我想将它转换为等效的XML。

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

遵循我期望的XML树结构

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

我尝试过很多东西,但没有运气。如果需要更多信息,我会提供更多信息。

4 个答案:

答案 0 :(得分:1)

尝试使用Array2XML(http://www.lalit.org/lab/convert-php-array-to-xml-with-attributes)这对我​​有用。

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


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

echo $xml;

输出

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

答案 1 :(得分:0)

这应该让你走上正确的轨道,可能需要一些调整(特别是关于键以及首先检查$ arr是否是一个数组/对象...这只是为了告诉你如何递归迭代它们并打印数据。)

此外,此代码段不支持任何方式的XML属性。

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

输出:

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

答案 2 :(得分:0)

我得到了类似以下代码的解决方案

<?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();

<强>输出

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

答案 3 :(得分:0)

我认为 - 这必须更简单:

function arrayToXML($arr){

  $s = "";

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

  return $s;}