0

I wanted to post an answer to this tread but it's either closed for answers or I don't have enough points to answer.

How to convert array to SimpleXML

I liked the answer by @Hanmant with the array_to_xml function he wrote. It was almost the answer I needed. I didn't like how it created child elements numbered "item0":

For example:

This is my sample Array:

$array = array();
$array["exampleOrderId"] = 1097374;
$array["timeStamp"] = date("Y-m-d", time());
$array["messages"][] = "Order Received";
$array["messages"][] = "Order Submitted For Processsing";
$array["events"][] = "Top Level Event";
$array["events"][] = "Moving order to processing queue";
$array["errors"][] = "Order item is not in stock";

Initial output:

<?xml version="1.0" encoding="UTF-8"?>
<orderResponse>
   <exampleOrderId>1097374</exampleOrderId>
   <timeStamp>2018-11-26</timeStamp>
   <messages>
      <item0>Order Received</item0>
      <item1>Order Submitted For Processsing</item1>
   </messages>
   <events>
      <item0>Top Level Event</item0>
      <message>
         <item0>Moving order to processing queue</item0>
         <error>
            <item0>Order item is not in stock</item0>
         </error>
      </message>
   </events>
</orderResponse>

There are elements here where messages is an array of message and errors is an array of error.

I made a very small change to his original array_to_xml function so it produces a child element which is the singular version of it's parent:

<?xml version="1.0" encoding="UTF-8"?>
<orderResponse>
   <exampleOrderId>1097374</exampleOrderId>
   <timeStamp>2018-11-26</timeStamp>
   <messages>
      <message>Order Received</message>
      <message>Order Submitted For Processsing</message>
   </messages>
   <events>
      <event>Top Level Event</event>
      <event>Moving order to processing queue</event>
   </events>
   <errors>
      <error>Order item is not in stock</error>
   </errors>
</orderResponse>

This is the revised function:

function array_to_xml( $data, &$xml_data, $element = "") {
    foreach( $data as $key => $value ) {
        if(! is_numeric($key) ){
            $element = $key;
        } else {
            $element = rtrim($element,'s');
        }

        if( is_array($value) ) {
            $subnode = $xml_data->addChild($element);
            array_to_xml($value, $subnode, $element);
        } else {
            $xml_data->addChild("$element",htmlspecialchars("$value"));
        }
    }
}
Rob Watts
  • 103
  • 4
  • The question you linked is "protected" because it already has 41 answers, and it's not necessary for Stack Overflow to include every possible version of this algorithm. – IMSoP Nov 28 '18 at 14:06
  • It's not a duplicate, the function is the same but the output is different from the original poster's function. I attempted to be clear in my post that the function is different (and related to the original) and I included examples of the original and revised outputs. – Rob Watts Nov 29 '18 at 15:05
  • The *answer* may be different, but the *question* is not; all of the text you have put as a question is actually part of your answer. If it is useful enough to add a 42nd answer to the existing question, you will be able to do so when you have earned 7 more reputation points, as the yellow box explains ("To answer it, you must have earned at least 10 reputation on this site (the association bonus does not count)."). However, think carefully about who you expect to read your new answer, given all the existing examples here and elsewhere. – IMSoP Nov 29 '18 at 15:23

1 Answers1

0

All I did here was add an optional argument to the function, $element. I used $element rather than $key in the addChild method.

The function assigns $element the value of $key as long its its not numeric. If it IS numeric, I just strip the plural 's' from the string.

This solution produces the XML people are using to handling in REST apis and such. It's weak in the sense that it requires some consideration in the construction of your array keys.

It works recursively just like the original authors.

As I've mentioned, I don't post much and don't really know how to use this site well. If I've quoted the wrong author of the original function I'm happy to fix that.

Rob Watts
  • 103
  • 4