Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert associative array to XML in PHP

Tags:

json

php

xml

I was wondering if a function capable of converting an associative array to an XML document exists in PHP (or some widely available PHP library).

I've searched quite a lot and could only find functions that do not output valid XML. I believe that the array I'm testing them on is correctly constructed, since it can be correctly used to generate a JSON document using json_encode. However, it is rather large and it is nested on four levels, which might explain why the functions I've tried so far fail.

Ultimately, I will write the code to generate the XML myself but surely there must be a faster way of doing this.

like image 840
Epicurus Avatar asked Apr 20 '11 15:04

Epicurus


5 Answers

function combArrToXML($arrC=array(), $root="root", $element="element"){
  $doc = new DOMDocument();
  $doc->formatOutput = true;

  $r = $doc->createElement( $root );
  $doc->appendChild( $r );

  $b = $doc->createElement( $element );
  foreach( $arrC as  $key => $val)
  {
    $$key = $doc->createElement( $key );
    $$key->appendChild(
      $doc->createTextNode( $val )
    );
    $b->appendChild( $$key );
    $r->appendChild( $b );
  }

  return $doc->saveXML();
}

Example:

$b=array("testa"=>"testb", "testc"=>"testd");
combArrToXML($b, "root", "element");

Output:

<?xml version="1.0"?>
<root>
  <element>
    <testa>testb</testa>
    <testc>testd</testc>
  </element>
</root>
like image 71
user1016724 Avatar answered Sep 28 '22 23:09

user1016724


Call

// $data = array(...);
$dataTransformator = new DataTransformator();
$domDocument = $dataTransformator->data2domDocument($data);
$xml = $domDocument->saveXML();

DataTransformator

class DataTransformator {

    /**
     * Converts the $data to a \DOMDocument.
     * @param array $data
     * @param string $rootElementName
     * @param string $defaultElementName
     * @see MyNamespace\Dom\DataTransformator#data2domNode(...)
     * @return Ambigous <DOMDocument>
     */
    public function data2domDocument(array $data, $rootElementName = 'data', $defaultElementName = 'item') {
        return $this->data2domNode($data, $rootElementName, null, $defaultElementName);
    }

    /**
     * Converts the $data to a \DOMNode.
     * If the $elementContent is a string,
     * a DOMNode with a nested shallow DOMElement
     * will be (created if the argument $node is null and) returned.
     * If the $elementContent is an array,
     * the function will applied on every its element recursively and
     * a DOMNode with a nested DOMElements
     * will be (created if the argument $node is null and) returned.
     * The end result is always a DOMDocument object.
     * The casue is, that a \DOMElement object
     * "is read only. It may be appended to a document,
     * but additional nodes may not be appended to this node
     * until the node is associated with a document."
     * See {@link http://php.net/manual/en/domelement.construct.php here}).
     * 
     * @param Ambigous <string, mixed> $elementName Used as element tagname. If it's not a string $defaultElementName is used instead.
     * @param Ambigous <string, array> $elementContent
     * @param Ambigous <\DOMDocument, NULL, \DOMElement> $parentNode The parent node is
     *  either a \DOMDocument (by the method calls from outside of the method)
     *  or a \DOMElement or NULL (by the calls from inside).
     *  Once again: For the calls from outside of the method the argument MUST be either a \DOMDocument object or NULL.
     * @param string $defaultElementName If the key of the array element is a string, it determines the DOM element name / tagname.
     *  For numeric indexes the $defaultElementName is used.
     * @return \DOMDocument
     */
    protected function data2domNode($elementContent, $elementName, \DOMNode $parentNode = null, $defaultElementName = 'item') {
        $parentNode = is_null($parentNode) ? new \DOMDocument('1.0', 'utf-8') : $parentNode;
        $name = is_string($elementName) ? $elementName : $defaultElementName;
        if (!is_array($elementContent)) {
            $content = htmlspecialchars($elementContent);
            $element = new \DOMElement($name, $content);
            $parentNode->appendChild($element);
        } else {
            $element = new \DOMElement($name);
            $parentNode->appendChild($element);
            foreach ($elementContent as $key => $value) {
                $elementChild = $this->data2domNode($value, $key, $element);
                $parentNode->appendChild($elementChild);
            }
        }
        return $parentNode;
    }
}
like image 29
automatix Avatar answered Oct 16 '22 21:10

automatix


No. At least there is no such in-built function. It's not a probrem to write it at all.

surely there must be a faster way of doing this

How do you represent attribute in array? I can assume keys are tags and values are this tags content.

Basic PHP Array -> JSON works just fine, cause those structure is... well... almost the same.

like image 24
Nemoden Avatar answered Oct 16 '22 22:10

Nemoden


I realize I am a Johnny-Come-Lately here, but I was working with the VERY same problem -- and the tutorials I found out there would almost (but not quite upon unit testing) cover it.

After much frustration and research, here is what I cam up with

XML To Assoc. Array:

From http://www.php.net/manual/en/simplexml.examples-basic.php

json_decode( json_encode( simplexml_load_string( $string ) ), TRUE );

Assoc. Array to XML

notes:

  • XML attributes are not handled
  • Will also handle nested arrays with numeric indices (which are not valid XML!)

From http://www.devexp.eu/2009/04/11/php-domdocument-convert-array-to-xml/

/// Converts an array to XML
/// - http://www.devexp.eu/2009/04/11/php-domdocument-convert-array-to-xml/
/// @param  <array> $array  The associative array you want to convert; nested numeric indices are OK!
function   getXml( array $array )  {

    $array2XmlConverter  = new XmlDomConstructor('1.0', 'utf-8');
    $array2XmlConverter->xmlStandalone   = TRUE;
    $array2XmlConverter->formatOutput    = TRUE;

    try {
        $array2XmlConverter->fromMixed( $array );
        $array2XmlConverter->normalizeDocument ();
        $xml    = $array2XmlConverter->saveXML();
//        echo "\n\n-----vvv start returned xml vvv-----\n";
//        print_r( $xml );
//        echo "\n------^^^ end returned xml ^^^----\n"
        return  $xml;
    }
    catch( Exception $ex )  {
//        echo "\n\n-----vvv Rut-roh Raggy! vvv-----\n";
//        print_r( $ex->getCode() );     echo "\n";
//        print_r( $->getMessage() );
//        var_dump( $ex );
//        echo "\n------^^^ end Rut-roh Raggy! ^^^----\n"
        return  $ex;
    }
}

... and here is the class to use for the $array2XmlConverter object:

/**
 * Extends the DOMDocument to implement personal (utility) methods.
 * - From: http://www.devexp.eu/2009/04/11/php-domdocument-convert-array-to-xml/
 * - `parent::` See http://www.php.net/manual/en/class.domdocument.php
 *
 * @throws   DOMException   http://www.php.net/manual/en/class.domexception.php
 *
 * @author Toni Van de Voorde
 */
class   XmlDomConstructor   extends DOMDocument {

    /**
     * Constructs elements and texts from an array or string.
     * The array can contain an element's name in the index part
     * and an element's text in the value part.
     *
     * It can also creates an xml with the same element tagName on the same
     * level.
     *
     * ex:
        \verbatim
             <nodes>
                <node>text</node>
                <node>
                    <field>hello</field>
                    <field>world</field>
                </node>
             </nodes>
        \verbatim
     *
     *
     * Array should then look like:
        \verbatim
             array(
                "nodes" => array(
                    "node" => array(
                        0 => "text",
                        1 => array(
                            "field" => array (
                                0 => "hello",
                                1 => "world",
                            ),
                        ),
                    ),
                ),
             );
        \endverbatim
     *
     * @param mixed $mixed An array or string.
     *
     * @param DOMElement[optional] $domElement Then element
     * from where the array will be construct to.
     *
     */
    public  function    fromMixed($mixed, DOMElement $domElement = null) {

        $domElement = is_null($domElement) ? $this : $domElement;

        if (is_array($mixed)) {
            foreach( $mixed as $index => $mixedElement ) {

                if ( is_int($index) ) {
                    if ( $index == 0 ) {
                        $node = $domElement;
                    } 
                    else {
                        $node = $this->createElement($domElement->tagName);
                        $domElement->parentNode->appendChild($node);
                    }
                }
                else {
                    $node = $this->createElement($index);
                    $domElement->appendChild($node);
                }

                $this->fromMixed($mixedElement, $node);
            }
        } 
        else {
            $domElement->appendChild($this->createTextNode($mixed));
        }
    }
} // end of class
like image 7
misterich Avatar answered Oct 16 '22 20:10

misterich


PHP's DOMDocument objects are probably what you are looking for. Here is a link to an example use of this class to convert a multi-dimensional array into an xml file - http://www.php.net/manual/en/book.dom.php#78941

like image 1
65Fbef05 Avatar answered Oct 16 '22 22:10

65Fbef05