当前位置:网站首页>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>
"
*/
边栏推荐
- Php/js cookie sharing across domains
- Balsamiq wireframes free installation
- Pytorch Basics
- 2022-2-14 learning xiangniuke project - Section 6 displays login information
- 简单封装 js并应用
- Minimum value ruler method for the length of continuous subsequences whose sum is not less than s
- Get the details of the next largest number
- 《CGNF: CONDITIONAL GRAPH NEURAL FIELDS》阅读笔记
- Fabric. JS compact JSON
- Fabric. JS basic brush
猜你喜欢

2022-2-14 learning xiangniuke project - Section 7 account setting

Ls1046nfs mount file system

测试 - 用例篇

软件测试 - 概念篇

Brew install * failed, solution

3D printer G code command: complete list and tutorial

Cambrian was reduced by Paleozoic venture capital and Zhike shengxun: a total of more than 700million cash

Lingyunguang rushes to the scientific innovation board: the annual accounts receivable reaches 800million. Dachen and Xiaomi are shareholders

Opencv LBP features

ThreadLocal memory leak
随机推荐
2022-2-15 learning xiangniuke project - Section 8 check login status
15 C language advanced dynamic memory management
2022-2-14 learning xiangniuke project - section 23, section 5, development login and exit functions
Fabric. JS iText set italics manually
Fabric. JS centered element
Software testing learning - day 4
OLED12864 液晶屏
Ubuntu 20.04 installing mysql8
Applet jumps to official account
JVM class loading mechanism
Operator details
在线音乐播放器app
Technologists talk about open source: This is not just using love to generate electricity
RGB 无限立方体(高级版)
Record sentry's path of stepping on the pit
Uva548 tree
1035 Password
Gee series: Unit 5 remote sensing image preprocessing [GEE grid preprocessing]
7. Eleven state sets of TCP
《CGNF: CONDITIONAL GRAPH NEURAL FIELDS》阅读笔记