您当前的位置:首页 > 计算机 > 编程开发 > PHP

php数组转化为xml

时间:03-11来源:作者:点击数:

php数组转化为xml

> 数组为

array(1) {
  ["View"]=>
  array(2) {
    ["attr"]=>
    array(4) {
      ["code"]=>
      string(15) "SystemInfo_View"
      ["version"]=>
      string(4) "1.00"
      ["id"]=>
      string(0) ""
      ["description"]=>
      string(12) "信息视图"
    }
    ["value"]=>
    array(1) {
      ["System"]=>
      array(2) {
        ["attr"]=>
        array(1) {
          ["type"]=>
          string(8) "hardware"
        }
        ["value"]=>
        array(4) {
          ["Classification"]=>
          array(2) {
            ["attr"]=>
            string(0) ""
            ["value"]=>
            string(12) "控制设备"
          }
          ["Name"]=>
          array(2) {
            ["attr"]=>
            string(0) ""
            ["value"]=>
            string(8) "XX设备"
          }
          ["Manufactyrer"]=>
          array(2) {
            ["attr"]=>
            string(0) ""
            ["value"]=>
            string(6) "中国"
          }
          ["Os"]=>
          array(2) {
            ["attr"]=>
            array(2) {
              ["type"]=>
              string(3) "Win"
              ["name"]=>
              string(7) "Windows"
            }
            ["value"]=>
            string(0) ""
          }
        }
      }
    }
  }
}

> 转化后的xml格式

<?xml version="1.0" encoding="UTF-8"?>
<ViewPack>
    <View code="SystemInfo_View" version="1.00" id="" description="信息视图">
    <System type="hardware">
        <Classification>控制设备</Classification>
        <Name>XX设备</Name>
        <Manufactyrer>中国</Manufactyrer>
        <Os type="Win" name="Windows"/>
    </System>
    </View>
</ViewPack>

> 代码实现

<?php 
/*
<?xml version="1.0" encoding="GBK"?>
<ViewPack>
    <View code="SystemInfo_View" version="1.00" id="" description="信息视图">
        <System type="hardware">
            <Classification>控制设备</Classification>
            <Name>XX设备</Name>
            <Manufacturer>中国</Manufacturer>
            <Os type="Windows" name="Windows Server 2008"/>
        </System>
    </View>
</ViewPack>
*/

function addConfigAttr($element, $data){
    echo "---------------function addConfigAttr()----------\n";
    var_dump($element);
    var_dump($data);
    if($element || $data){
        foreach($data as $key=>$value){
            $element->addAttribute($key, $value);            
        }
    }
}

function createPack($element, $data=array())
{
    echo "---------------function createPack()----------\n";
    // var_dump($element);
    // var_dump($data);
    if($element || $data)   
    {
        if(is_array($data)){
            foreach($data as $key=>$value){
                echo(is_array($value['value']));
                echo(!is_numeric($key));

                if(is_array($value['value']) && !is_numeric($key)){  //如果key不是数字,value的value是数组
                   echo "*11111";
                   $child = $element->addChild($key);   //给simpleXMLElement添加子元素
                   if($value['attr']){
                       addConfigAttr($child, $value['attr']);
                   }
                   createPack($child, $value['value']); //递归该函数
               }elseif(is_array($value) && !isset($value['value'])){ 
                   echo "*22222";
                   foreach ($value as $vk=>$vv){
                        if(isset($vv['attr'][0])){
                            foreach ($value['attr'] as $av){
                                $child = $element->addChild($key);
                                addConfigAttr($child, $av);
                            }
                        }else{
                            if(is_numeric($vk)) {
                                if(is_array($vv)) {
                                    foreach($vv as $k1 => $v1) {
                                        $child = $element->addChild($k1);
                                        addConfigAttr($child, $v1['attr']);
                                    }
                                }
                            } else {
                                $child = $element->addChild($vk);
                                addConfigAttr($child, $vv['attr']);
                            }
                        }
                        createPack($child, $vv['value']);
                    }
               }elseif(empty($value['value']) && $value['attr'][0]){
                   echo "*33333";
               }else{
                   echo "*44444";
                   $child = $element->addChild($key, $value['value']);  //给节点添加子元素
                   if($value['attr']){
                       addConfigAttr($child, $value['attr']);       //给子元素添加属性
                   }    
               }
            }
        }
    }
}

$config =  array('Classification'=>'控制设备','Name'=>'XX设备','Manufacturer'=>'中国', 'osType'=>'Win');
$simpleXML = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><ViewPack></ViewPack>');

$codearr=array();
$codearr['code'] = "SystemInfo_View";
$codearr['version'] = "1.00";
$codearr['id'] = "";
$codearr['description'] = "信息视图";

$systemValue = array();
$systemValue = array('Classification'=>array('attr'=>'', 'value'=>$config['Classification']),
                    'Name'=>array('attr'=>'', 'value'=>$config['Name']),
                    'Manufactyrer'=>array('attr'=>'', 'value'=>$config['Manufacturer']),
                    'Os'=>array('attr'=>array('type'=>$config['osType'], 'name'=>'Windows'), 'value'=>''));

$systemAttr = array('type'=>'hardware');
$viewValue = array('System'=>array('attr'=>$systemAttr, 'value'=>$systemValue));

$ret = array('View'=>array('attr'=>$codearr, 'value'=>$viewValue));
var_dump($ret);
createPack($simpleXML, $ret);
$xml = $simpleXML->asXML();
var_dump($xml);
?>

> 结果显示:

/*---------------function createPack()----------
array(1) {
  ["View"]=>
  array(2) {
    ["attr"]=>
    array(4) {
      ["code"]=>
      string(15) "SystemInfo_View"
      ["version"]=>
      string(4) "1.00"
      ["id"]=>
      string(0) ""
      ["description"]=>
      string(12) "信息视图"
    }
    ["value"]=>
    array(1) {
      ["System"]=>
      array(2) {
        ["attr"]=>
        array(1) {
          ["type"]=>
          string(8) "hardware"
        }
        ["value"]=>
        array(4) {
          ["Classification"]=>
          array(2) {
            ["attr"]=>
            string(0) ""
            ["value"]=>
            string(12) "控制设备"
          }
          ["Name"]=>
          array(2) {
            ["attr"]=>
            string(0) ""
            ["value"]=>
            string(8) "XX设备"
          }
          ["Manufactyrer"]=>
          array(2) {
            ["attr"]=>
            string(0) ""
            ["value"]=>
            string(6) "中国"
          }
          ["Os"]=>
          array(2) {
            ["attr"]=>
            array(2) {
              ["type"]=>
              string(3) "Win"
              ["name"]=>
              string(7) "Windows"
            }
            ["value"]=>
            string(0) ""
          }
        }
      }
    }
  }
}



11*11111---------------function addConfigAttr()----------
object(SimpleXMLElement)#2 (0) {
}
array(4) {
  ["code"]=>
  string(15) "SystemInfo_View"
  ["version"]=>
  string(4) "1.00"
  ["id"]=>
  string(0) ""
  ["description"]=>
  string(12) "信息视图"
}
---------------function createPack()----------
11*11111---------------function addConfigAttr()----------
object(SimpleXMLElement)#3 (0) {
}
array(1) {
  ["type"]=>
  string(8) "hardware"
}
---------------function createPack()----------
1*444441*444441*444441PHP Notice:  Undefined offset: 0 in /root/桌面/php/6_creact_xml_pack.php on line 69
*44444---------------function addConfigAttr()----------
object(SimpleXMLElement)#5 (0) {
}
array(2) {
  ["type"]=>
  string(3) "Win"
  ["name"]=>
  string(7) "Windows"
}
string(310) "
<?xml version="1.0" encoding="UTF-8"?>
<ViewPack>
    <View code="SystemInfo_View" version="1.00" id="" description="信息视图">
    <System type="hardware">
        <Classification>控制设备</Classification>
        <Name>XX设备</Name>
        <Manufactyrer>中国</Manufactyrer>
        <Os type="Win" name="Windows"/>
    </System>
    </View>
</ViewPack>
"
*/
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门