BIG

PHP 를 이용하여 배열을 XML 문서 문자열로 변환샘플

 

<?php
function array_to_xml( $arr, &$xml, $pk=null )
{
    foreach( $arr as $k => $v )
    {
        if( !is_array($v) )
        {
            $xml->addChild($k,htmlspecialchars($v));
        }else if( is_numeric(key($v)) ){
            array_to_xml($v, $xml, $k);
        }else{
            array_to_xml($v, $xml->addChild( is_null($pk)? $k: $pk));
        }// end if
    }// end foreach
        
}// end function
    
$xml    =   simplexml_load_string(
                      ''
                     ,'SimpleXMLElement'
                     ,LIBXML_NOCDATA
                );
$arr        =    array(
                            'title'    =>    'test'
                            ,'comment'    =>    'test_comment'
                            ,data    =>    array(
                                                       array('item' => 'A')
                                                      ,array('item' => 'B')
                                                      ,array('item' => 'C')
                                                      ,array('item' => 'D')
                                                      ,array('item' => 'E')
                                                      )
                );
                
    array_to_xml( $arr, &$xml, $pk=null );


?>
LIST