简体   繁体   English

如何在php中将数组转换为XML

[英]How to convert Array to XML in php

I´m writing a php that should convert an array into xml-structure. 我正在编写一个将数组转换为xml结构的php。

My Problem is that I can´t get the value of the array in my xml-structure. 我的问题是我无法在xml结构中获取数组的值。 Probably just a small mistake but I don´t find it. 可能只是一个小错误,但我找不到。

Here´s my code: 这是我的代码:

<?php

$format = strtolower(@$_GET['format']) == 'json' ? 'json' : 'xml'; //xml ist default

if (isset($_GET['insert_nr']) and $_GET['insert_nr']<>"") $insert_nr=$_GET['insert_nr']; else $insert_nr="4051101000";

$code = $insert_nr;

codegenerator($code, $format);

function codegenerator($code, $format)
{

// Here is some code missing that generates the array $final_array[]


$final_array[] = array(
    'var1' => $ans1["$dub1"],
    'var2' => $ans2,
    'var3' => $ans3,
    'var4' => $and3["$dub4"]);

$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($final_array)); 

if($format == 'json') {
            header('Content-type: application/json');
            echo json_encode(array('return_list'=>$it));
        }

            else {
            header('Content-type: text/xml');
            echo '<list>';
            foreach($it as $key => $value) {
                echo '<',$key,'>';
                if(is_array($value)) {
                    echo '<',$value,'>',htmlentities($value),'</',$value,'>';
                    foreach($value as $tag => $val) {
                        echo '<',$tag,'>',htmlentities($val),'</',$tag,'>';
                    }
                }
                echo '</',$key,'>';
            }
        echo '</list>';
        }

}

?>

This is what my xml looks like: 这是我的xml的样子:

<list>
   <var1/>
   <var2/>
   <var3/>
   <var4/>
<list>

Why can´t I get the answers of the variables. 为什么我无法获得变量的答案。 It should look like this: 它看起来应该像这样:

<list>
   <var1>xyz</var1>
   <var2>4</var2>
   <var3>34,0</var3>
   <var4>abc</var4>
</list>

You're missing the else block. 您缺少else块。 After this code: 此代码后:

if(is_array($value)) {
    echo '<',$value,'>',htmlentities($value),'</',$value,'>';
    ........
}

add this: 添加:

} else {
    echo $value;
}

Also I would recommend writing a recursive function to be able to use arrays of any depth. 我也建议编写一个递归函数,以便能够使用任何深度的数组。

Try the below code: 试试下面的代码:

//function defination to convert array to xml
function array_to_xml($array, &$xml_user_info) {
    foreach($array as $key => $value) {
        if(is_array($value)) {
            if(!is_numeric($key)){
                $subnode = $xml_user_info->addChild("$key");
                array_to_xml($value, $subnode);
            }else{
                $subnode = $xml_user_info->addChild("item$key");
                array_to_xml($value, $subnode);
            }
        }else {
            $xml_user_info->addChild("$key",htmlspecialchars("$value"));
        }
    }
}

//creating object of SimpleXMLElement
$xml_user_info = new SimpleXMLElement("<?xml version=\"1.0\"?><user_info></user_info>");

//function call to convert array to xml
array_to_xml($users_array,$xml_user_info);

//saving generated xml file
$xml_file = $xml_user_info->asXML('users.xml');

//success and error message based on xml creation
if($xml_file){
    echo 'XML file have been generated successfully.';
}else{
    echo 'XML file generation error.';
}

For reference visit https://www.codexworld.com/convert-array-to-xml-in-php/ 有关参考,请访问https://www.codexworld.com/convert-array-to-xml-in-php/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM