• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Array of arrays into XML

#1
[eluser]vindhyareddy[/eluser]
Hi people

Can someone help me in converting an array of arrays into xml document?


Array
(
[info_1] => Array
(
[val1]=>..
[val2]=>..
)

[info_2] => Array
(
[val1]=>..
[val2]=>..
)

[info_3] => Array
(
[val1]=>..
[val2]=>..
)

[info_4] => Array
(
[val1]=>..
[val2]=>..
)
)

how do I make this into XML??

Help needed asap!

#2
[eluser]Kamarg[/eluser]
I use this. You'll need to replace append_child, create_element, and create_text_node with your own xml creation functions.

Code:
function _arrayToXml($parent, $children, $root_node_name='data') {
    $numeric = FALSE;
    // loop through the data passed in.
    foreach($children as $key => $value) {
        // no numeric keys per xml spec. make string a key
        if(is_numeric($key)) {
            $numeric = TRUE;
            $key = $root_node_name;
        }

        // replace anything not alpha numeric/underscore
        $key = preg_replace('/[^a-z0-9_]/i', '', $key);
        $node = create_element($this->_dom, $key);
        
        // if there is another array found, recursively call this function
        if(is_array($value)) {
            if($numeric || is_assoc($value)) {
                append_child($parent, $node);
                $this->_arrayToXml($node, $value, $key);
            } else {
                $this->_arrayToXml($parent, $value, $key);
            }
        } else {
            // add single node
            $text_node = create_text_node($this->_dom, $value);
            append_child($node, $text_node);
            append_child($parent, $node);
        }
    }
}

function is_assoc($array) {
    return (is_array($array) && (0 !== count(array_diff_key($array, array_keys(array_keys($array)))) || count($array)==0));
}

#3
[eluser]vindhyareddy[/eluser]
Where am I passing my array???

Whats the parent and the child parameters in the function

#4
[eluser]Kamarg[/eluser]
Whoops. That's what happens when you post in a hurry. Forgot a few of the important details. The first parameter will be your DOMDocument (or you can pass a previously created DOMNode to append in the middle of an xml document), the second will be your array.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.