1

In CakePHP I know the XML-class in cake.libs. What I like on this easy to use class is the fact that you can convert an array to xml with attributes.
On a current project I'm working with Zend Framework and I am missing this nice class.

Does anybody know how I can convert an PHP-array to XML with attributes easily?

I tried this one but unfortunately its not easy to handle result arrays from database cause you have to define the attributes inside the array.

Maybe I have missed something in ZF? Or does anybody know how to adapt the CakePHP class to ZF?

Many thanks for any helpful hints.

Stephan Weinhold
  • 1,623
  • 1
  • 28
  • 37
MadeOfSport
  • 513
  • 1
  • 7
  • 19
  • "Easily" is very subjective. Have you tried anything yet...? – netcoder Oct 28 '11 at 20:27
  • yes things like mentioned http://www.lalit.org/lab/convert-php-array-to-xml-with-attributes http://stackoverflow.com/questions/1397036/how-to-convert-array-to-simplexml http://www.devexp.eu/2009/04/11/php-domdocument-convert-array-to-xml/ – MadeOfSport Oct 28 '11 at 20:37
  • Why not just using the CakePHP 3.x Xml class then? You can use the Utility split off using composer super-easily in any (even non cake) project. – mark May 05 '15 at 12:16

3 Answers3

2

This function will allow you to create XML document with simple PHP array variable. Nodes are just stacked onto each other and naming it 'Attribute_XXX' will add atribute XXX to that parent node.

function to_xml(SimpleXMLElement $object, array $data)
{
    $attr = "Attribute_";
    foreach ($data as $key => $value) {
        if (is_array($value)) {
            $new_object = $object->addChild($key);
            to_xml($new_object, $value);
        } else {
            if(strpos($key, $attr) !== false){
                $object->addAttribute(substr($key, strlen($attr)), $value);
            }else{
                $object->addChild($key, $value);
            }
        }
    }
}

Usage:

$my_array = array (
    'TagN1' => array (
        'TagInsideOfIt' => array (
            'Atrribute_IDuser' => 'anything',
            'RegularTag' => 'whatever',
            'Address' => array(
                'Attribute_ID' => '111',
                'Attribute_key' => 'aaa',
                'Company' => 'Google Inc.'
            )
        )
    )
);

$xml = new SimpleXMLElement('<root/>');

to_xml($xml, $my_array);

Header('Content-type: text/xml');
print($xml->asXML());
jezda159
  • 33
  • 8
0

You can get some answers at this URL How to convert array to SimpleXML

An alternative/fast and easy approach would be to convert array data into JSON, that would be faster as well using Zend library. Converting array into JSON is very easy as using follows functions.

Zend_Json::encode() and Zend_Json_Encoder::encode()
Community
  • 1
  • 1
Krishan Gopal
  • 4,073
  • 1
  • 16
  • 19
0

Tanks, a using the code of @jezda159 and i modified for me, this is my code:

the array:

   $elements_array = array(
            "node" => array(
                "subnode" => array(
                    "[attribute]" => "valueOfAttribute",
                    "[second-attribute]" => "valueOfAttribute",
                    "second-subnode" => array(
                        "[attribute]" => "valueOfAttribute",
                        "[second-attribute]" => "valueOfAttribute",
                    ),
                    "ListOfElements" => array(
                        "one",
                        "two",
                        "tree"
                    )
                )
            ),
        );

The code:

    function array_to_xml($data, $object){
        foreach ($data as $key => $value) {
            $keyname = is_numeric( $key ) ? "item".$key : $key;
            if (is_array($value)) {
                $new_object = $object->addChild($keyname);
                array_to_xml($value, $new_object);
            } else {
                preg_match("#\[([a-z0-9-_]+)\]#i", $keyname, $attr);
                if( count($attr) ){
                    $object->addAttribute($attr[1], $value);
                }else{
                    $object->addChild($keyname, $value);
                }
            }
        }
    }
$xml_user_info = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" ?><Elements></Elements>');

//function call to convert array to xml
array_to_xml($elements_array,$xml_user_info);
header('Content-type: text/xml');
//show generated xml file
echo $xml_user_info->asXML();

The result:

<?xml version="1.0" encoding="UTF-8"?>
<Elements>
<node>
<subnode attribute="valueOfAttribute" second-attribute="valueOfAttribute">
<second-subnode attribute="valueOfAttribute" second-attribute="valueOfAttribute"/>
<ListOfElements>
<item0>one</item0>
<item1>two</item1>
<item2>tree</item2>
</ListOfElements>
</subnode>
</node>
</Elements>
Jorhel Reyes
  • 113
  • 1
  • 8