当前位置:网站首页>php数组转化为xml
php数组转化为xml
2022-07-02 05:47:00 【杰儿__er】
> 数组为
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>
"
*/
边栏推荐
- 1036 Boys vs Girls
- KMP idea and template code
- Résumé de la collection de plug - ins couramment utilisée dans les outils de développement idea
- 15 C language advanced dynamic memory management
- GRBL 软件:简单解释的基础知识
- 1036 Boys vs Girls
- Php/js cookie sharing across domains
- The Hong Kong Stock Exchange learned from US stocks and pushed spac: the follow-up of many PE companies could not hide the embarrassment of the world's worst stock market
- 【pyinstaller】_ get_ sysconfigdata_ name() missing 1 required positional argument: ‘check_ exists‘
- Detailed explanation of Pointer use
猜你喜欢
3D printer G code command: complete list and tutorial
Centos8 installation mysql8.0.22 tutorial
Win10 copy files, save files... All need administrator permission, solution
brew install * 失败,解决方法
2022-2-14 learning xiangniuke project - Section 6 displays login information
Gee series: unit 6 building various remote sensing indexes in Google Earth engine
Fabric. JS centered element
Cube magique infini "simple"
15 C language advanced dynamic memory management
Gcnet: non - local Networks meet Squeeze excitation Networks and Beyond
随机推荐
1037 Magic Coupon
Pytorch Basics
Cube magique infini "simple"
Zzuli:1069 learn from classmate Z
Opencv LBP features
Fabric. JS right click menu
Generate QR code
Fabric. JS iText sets the color and background color of the specified text
数据挖掘方向研究生常用网站
460. LFU 缓存 双向链表
Here comes a new chapter in the series of data conversion when exporting with easyexcel!
all3dp. All Arduino projects in com website (2022.7.1)
Grbl software: basic knowledge of simple explanation
【論文翻譯】GCNet: Non-local Networks Meet Squeeze-Excitation Networks and Beyond
Zzuli: maximum Convention and minimum common multiple
Gee series: unit 6 building various remote sensing indexes in Google Earth engine
Centos8 installation mysql8.0.22 tutorial
正则表达式总结
Thread pool overview
TypeScript的泛型和泛型约束