Jump to content

php array 2 xml and xml 2 array ?


feha

Recommended Posts

Hi

I have seen many classes but could not find a php class that could convert this array to XML:

Array
(
    [1] => Array
        (
            [id] => 1
            [name] => Articles
            [class] => articles
            [link] => #
            [active] => 1
            [group_id] => 0
            [parent] => 0
            [children] => Array
                (
                    [5] => Array
                        (
                            [id] => 5
                            [name] => Add new
                            [class] => add_article
                            [link] => #
                            [active] => 1
                            [group_id] => 0
                            [parent] => 1
                            [children] => Array
                                (
                                )

                        )

                    [6] => Array
                        (
                            [id] => 6
                            [name] => Categories
                            [class] => categories
                            [link] => #
                            [active] => 1
                            [group_id] => 0
                            [parent] => 1
                            [children] => Array
                                (
                                )

                        )

                    [8] => Array
                        (
                            [id] => 8
                            [name] => Delete
                            [class] => delete
                            [link] => #
                            [active] => 1
                            [group_id] => 0
                            [parent] => 1
                            [children] => Array
                                (
                                )

                        )

                )

        )

    [2] => Array
        (
            [id] => 2
            [name] => Users
            [class] => users
            [link] => #
            [active] => 1
            [group_id] => 0
            [parent] => 0
            [children] => Array
                (
                    [7] => Array
                        (
                            [id] => 7
                            [name] => Add new
                            [class] => add_user
                            [link] => #
                            [active] => 1
                            [group_id] => 0
                            [parent] => 2
                            [children] => Array
                                (
                                )

                        )

                    [9] => Array
                        (
                            [id] => 9
                            [name] => Show
                            [class] => show
                            [link] => #
                            [active] => 1
                            [group_id] => 0
                            [parent] => 2
                            [children] => Array
                                (
                                    [10] => Array
                                        (
                                            [id] => 10
                                            [name] => Last created
                                            [class] => last
                                            [link] => #
                                            [active] => 1
                                            [group_id] => 0
                                            [parent] => 9
                                            [children] => Array
                                                (
                                                )

                                        )

                                    [11] => Array
                                        (
                                            [id] => 11
                                            [name] => First created
                                            [class] => first
                                            [link] => #
                                            [active] => 1
                                            [group_id] => 0
                                            [parent] => 9
                                            [children] => Array
                                                (
                                                )

                                        )

                                    [12] => Array
                                        (
                                            [id] => 12
                                            [name] => All
                                            [class] => all
                                            [link] => #
                                            [active] => 1
                                            [group_id] => 0
                                            [parent] => 9
                                            [children] => Array
                                                (
                                                )

                                        )

                                    [13] => Array
                                        (
                                            [id] => 13
                                            [name] => None
                                            [class] => none
                                            [link] => #
                                            [active] => 1
                                            [group_id] => 0
                                            [parent] => 9
                                            [children] => Array
                                                (
                                                )

                                        )

                                )

                        )

                )

        )

    [3] => Array
        (
            [id] => 3
            [name] => Groups
            [class] => groups
            [link] => #
            [active] => 1
            [group_id] => 0
            [parent] => 0
            [children] => Array
                (
                )

        )

    [4] => Array
        (
            [id] => 4
            [name] => Settings
            [class] => settings
            [link] => #
            [active] => 1
            [group_id] => 0
            [parent] => 0
            [children] => Array
                (
                )

        )

)

A tree structure, i need a way to make an xml structure from this but also get back from xml into array with same tree structure ...

Any help ?

Thank you in advance.

Link to comment
https://forums.phpfreaks.com/topic/128952-php-array-2-xml-and-xml-2-array/
Share on other sites

This is from my xml helper class. Try it and let me know if it works :D

 

public static function arraytoxml(array $data,$file,$elementname = "items") {
        $xml = simplexml_load_string(self::xmlcreate($file)); //we will always create the file

	if (!isset($xml->$elementname))
		$xml->addChild($elementname);				
	//Now let's add the data
	self::_arraytoxml($xml->$elementname,$data);	

	//print_r($xml);
	return self::xmltofile($xml,$file);		
}
//
/**
 * This is what really creates the array
 * I had to seperate it so that I could use recursion 
 * The values of the array, might be values them selfves
 *
 * @param SimpleXMLElement $xml
 * @param mixed $data
 */
private static function _arraytoxml(SimpleXMLElement &$xml,$data) {
        
	foreach ($data as $k => $value)	{
		if (is_numeric($k))
			$k = "_" . $k . "_"; //xml does not allow elements to just be numeric

		if (!isset($xml->$k))
			$xml->addChild($k);

		if (is_array($value))
			self::_arraytoxml($xml->$k,$value);
		else 
			$xml->$k = self::xmlCdata($value);
	}				
}	

Stupid edit timer!!

 

The xmlcreate is just a simple method to create a dom object and then create an xml file

 

public static function xmlcreate($file,$root = "root") {		
	$dom = new DOMDocument('1.0', 'utf-8');
	$element = $dom->appendChild(new DOMElement($root));

	$xml = $dom->saveXML();
	file_put_contents($file,$xml); 
	return $xml;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.