简体   繁体   中英

How to convert Array to XML in php

I´m writing a php that should convert an array into xml-structure.

My Problem is that I can´t get the value of the array in my xml-structure. 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:

<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. 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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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