1

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.

1
  • Down voted please comment what you want or give suggession for downvote Commented Aug 20, 2014 at 5:30

4 Answers 4

1

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>
Sign up to request clarification or add additional context in comments.

3 Comments

I tried but give me error like XML Parsing Error: no element found
Are you using firefox? Have a look at stackoverflow.com/questions/975929/….
In google chrome also not work and I am getting This XML file does not appear to have any style information associated with it. The document tree is shown below.
0

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>

3 Comments

Can I set header like <?xml version="1.0" encoding="utf-8"?> because I want to save file as XML
Your code give correct output but do not display header as I mentioned above comment so how I can set header?
You should be able to send the Content-Type using PHPs header() function. As I mentioned though: This is not a fully fleshed XML implementation. Attributes won't work, keys are missing sanity-checks and there's probably a lot more to it.
0

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>

Comments

0

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

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.