当前位置:网站首页>PHP array to XML
PHP array to XML
2022-07-02 05:49:00 【Jill__ er】
> The array is
array(1) {
["View"]=>
array(2) {
["attr"]=>
array(4) {
["code"]=>
string(15) "SystemInfo_View"
["version"]=>
string(4) "1.00"
["id"]=>
string(0) ""
["description"]=>
string(12) " Information view "
}
["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) " Control equipment "
}
["Name"]=>
array(2) {
["attr"]=>
string(0) ""
["value"]=>
string(8) "XX equipment "
}
["Manufactyrer"]=>
array(2) {
["attr"]=>
string(0) ""
["value"]=>
string(6) " China "
}
["Os"]=>
array(2) {
["attr"]=>
array(2) {
["type"]=>
string(3) "Win"
["name"]=>
string(7) "Windows"
}
["value"]=>
string(0) ""
}
}
}
}
}
}
> Transformed xml Format
<?xml version="1.0" encoding="UTF-8"?>
<ViewPack>
<View code="SystemInfo_View" version="1.00" id="" description=" Information view ">
<System type="hardware">
<Classification> Control equipment </Classification>
<Name>XX equipment </Name>
<Manufactyrer> China </Manufactyrer>
<Os type="Win" name="Windows"/>
</System>
</View>
</ViewPack>
> Code implementation
<?php
/*
<?xml version="1.0" encoding="GBK"?>
<ViewPack>
<View code="SystemInfo_View" version="1.00" id="" description=" Information view ">
<System type="hardware">
<Classification> Control equipment </Classification>
<Name>XX equipment </Name>
<Manufacturer> China </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)){ // If key Not numbers ,value Of value It's an array
echo "*11111";
$child = $element->addChild($key); // to simpleXMLElement Add child elements
if($value['attr']){
addConfigAttr($child, $value['attr']);
}
createPack($child, $value['value']); // Recurse this function
}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']); // Add child elements to nodes
if($value['attr']){
addConfigAttr($child, $value['attr']); // Add attributes to child elements
}
}
}
}
}
}
$config = array('Classification'=>' Control equipment ','Name'=>'XX equipment ','Manufacturer'=>' China ', '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'] = " Information view ";
$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);
?>
> Results show :
/*---------------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) " Information view "
}
["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) " Control equipment "
}
["Name"]=>
array(2) {
["attr"]=>
string(0) ""
["value"]=>
string(8) "XX equipment "
}
["Manufactyrer"]=>
array(2) {
["attr"]=>
string(0) ""
["value"]=>
string(6) " China "
}
["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) " Information view "
}
---------------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/ desktop /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=" Information view ">
<System type="hardware">
<Classification> Control equipment </Classification>
<Name>XX equipment </Name>
<Manufactyrer> China </Manufactyrer>
<Os type="Win" name="Windows"/>
</System>
</View>
</ViewPack>
"
*/
边栏推荐
- [technical notes-08]
- 《CGNF: CONDITIONAL GRAPH NEURAL FIELDS》阅读笔记
- Zzuli:1067 faulty odometer
- H5 jump applet
- Vscode paste image plugin saves image path settings
- Matplotlib double Y axis + adjust legend position
- Fabric. JS gradient
- 1036 Boys vs Girls
- Zzuli: maximum Convention and minimum common multiple
- 【pyinstaller】_ get_ sysconfigdata_ name() missing 1 required positional argument: ‘check_ exists‘
猜你喜欢
![[golang syntax] be careful with the copy of slices](/img/5e/1c82c58940939b94d03377ebdc03e3.jpg)
[golang syntax] be careful with the copy of slices

brew install * 失败,解决方法

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

Thunder on the ground! Another domestic 5g chip comes out: surpass Huawei and lead the world in performance?

Record sentry's path of stepping on the pit

OLED12864 液晶屏

Visual studio import

5g market trend in 2020

"Simple" infinite magic cube

Cambrian was reduced by Paleozoic venture capital and Zhike shengxun: a total of more than 700million cash
随机推荐
青训营--数据库实操项目
c语言中的几个关键字
Principle and implementation of parallax effect
How to change the IP address of computer mobile phone simulator
TypeScript的泛型和泛型约束
all3dp. All Arduino projects in com website (2022.7.1)
Zzuli: maximum Convention and minimum common multiple
php父类(parent)
kmp思想及模板代码
Fabric. JS 3 APIs to set canvas width and height
软件测试基础篇
Huawei Hongmeng OS, is it OK?
Matplotlib double Y axis + adjust legend position
Zzuli:1066 character classification statistics
php内类名称与类内方法名相同
Résumé de la collection de plug - ins couramment utilisée dans les outils de développement idea
GRBL 软件:简单解释的基础知识
Determine whether there is an element in the string type
[PHP是否安装了 SOAP 扩]对于php实现soap代理的一个常见问题:Class ‘SoapClient‘ not found in PHP的处理方法
php继承(extends)