0

I need to convert following input array into the xml.

Input Array:

    $inputData = array (   
      'name' => 'xml',
      'attr' => array(
        'id' => 123
       ),
      'children' => array(
         array(
          'name' => 'name',
          'attr' => array(),
          'children' => array(
            'Vegi List'
          ),
       ),
       array(
        'name' => 'vegitables',
        'attr' => array(),
        'children' => array(
            array(
                'name' => "vegitable",
                'attr' => array(
                    'id' => 456
                ),
                'children' => array(
                    array(
                        'name' => "name",
                        'attr' => array(),
                        'children' => array(
                            "Spinach"
                        )
                    )
                )
            ),
            array(
                'name' => "vegitable",
                'attr' => array(
                    'id' => 789
                ),
                'children' => array(
                    array(
                        'name' => "name",
                        'attr' => array(),
                        'children' => array(
                            "Carrot"
                        )
                    )
                )
            )
        )
    )
)

);

Output XML:

    <xml id="123456">
      <name>Vegi List</name>
        <vegitables>
          <vegitable id="5235632">
            <url>Spinach</url>
          </vegitables>
          <vegitables id="5235633">
           <url>Carrot</url>
          </vegitables>
        </vegitables>
     </xml>

I am thinking to use a recursive function but as it is complex array, I don't know that right ways to approach it.

Any help would be appreciated!

Thanks

2
  • Is it PHP? If so, add it to the tags. Commented Aug 28, 2016 at 12:06
  • Yes, this is php. Commented Aug 28, 2016 at 12:59

1 Answer 1

1

This looks like someone converted XML into a specific JSON format that keeps most of the node informations of a wellformed XML (without namespace support).

You are right that you will need an recursive function, but the XML format is not that complex. Basically you have an array that describes an XML element node with its name, attributes and children. The children are strings for text nodes or arrays for element node definitions.

That kind of format can be converted back into XML using DOM easily.

function appendNode(DOMNode $parent, $nodeDefinition) {
  $document = $parent instanceOf DOMDocument ? $parent : $parent->ownerDocument; 
  if (is_string($nodeDefinition)) {
    // append string as text node
    $node = $parent->appendChild(
      $document->createTextNode($nodeDefinition)
    );
  } elseif (is_array($nodeDefinition)) {
    // append array as new element
    $node = $parent->appendChild(
      $document->createElement($nodeDefinition['name'])
    );
    foreach ($nodeDefinition['attr'] as $name => $value) {
      $node->setAttribute($name, $value);
    }
    foreach ($nodeDefinition['children'] as $childDefinition) {
      // iterate child definitions and append them
      appendNode($node, $childDefinition);
    }
  }
}

$document = new DOMDocument();
appendNode($document, $inputData);

$document->formatOutput = TRUE;
echo $document->saveXml();

Output:

<?xml version="1.0"?>
<xml id="123">
  <name>Vegi List</name>
  <vegitables>
    <vegitable id="456">
      <name>Spinach</name>
    </vegitable>
    <vegitable id="789">
      <name>Carrot</name>
    </vegitable>
  </vegitables>
</xml>
Sign up to request clarification or add additional context in comments.

2 Comments

Thank. It works like a charm. Now I will try to recreate this without using DOMDocument.
Why? I suppose XMLWriter would be an alternative, but slightly more complex.

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.