How can generate XML file dynamically using PHP?

Votes : 3
1099
I need to source code to generate dynamic xml file at runtime. Please help me in generating the below XML file dynamically using PHP.
<?xml version="1.0" encoding="UTF-8"?>
<xml>
    <user>
        <real_name>Sophia  Mason</real_name>
        <email>sophia@gmail.com</email>
    </user>
    <user>
        <real_name>Emily Noah</real_name>
        <email>emily@ymail.com</email>
    </user>
    <user>
        <real_name>Natalie Liam</real_name>
        <email>natalie@codrate.com</email>
    </user>
    <user>
        <real_name>Anna Christian</real_name>
        <email>anna@yahoo.com</email>
    </user>
    <user>
        <real_name>Taylor Landon</real_name>
        <email>taylor@codrate.com</email>
    </user>
</xml>

Your Answer

1 Answer

answered
First need data array like that is you want to create xml file. Like here :
$users = array();
$users[] = array(
    'real_name' => 'Sophia  Mason',
    'email' => 'sophia@gmail.com',
);
$users[] = array(
    'real_name' => 'Emily Noah',
    'email' => 'emily@ymail.com',
);
$users[] = array(
    'real_name' => 'Natalie Liam',
    'email' => 'natalie@codrate.com',
);
$users[] = array(
    'real_name' => 'Anna Christian',
    'email' => 'anna@yahoo.com',
);
$users[] = array(
    'real_name' => 'Taylor Landon',
    'email' => 'taylor@codrate.com',
);
Then call following function.
function es_xmlwrite($xml, $types ,$file_path) {
    if (is_dir(dirname($file_path))) {
        $handle = @fopen($file_path, 'w+');
        if ($handle) {
            @fwrite($handle, "<!--?xml version=\"1.0\" encoding=\"utf-8\"?-->" . PHP_EOL);
            $lines = '';
            foreach($xml as $values){
                $lines .= "<$types>". PHP_EOL;
                foreach($values as $key => $value){
                     $lines .= "<$key>$value<!--$key-->". PHP_EOL;
                }
                $lines .= "<!--$types-->". PHP_EOL;
            }
            @fwrite($handle, "<xml>" . PHP_EOL. $lines . "</xml>");
            @fclose($handle);
            return TRUE;
        }
    }
}
es_xmlwrite($users, 'user' ,'user.xml'); // genarate xml file as you wish.

What is the codrate ?

codrate.com is a standard, fast cross browsing and highly versatile site. It is useful for many large number of Program Development Industries. So you can get support form Codrators , who are the codrate's joiners around world to help your program developments, You can answer other codrator's questions. Communicate with them. Share your knowledge with them. Do you have an interest in programming, So publish your articles about programming. It will help to maintain your professional co-profile. Actually codrate.com is not such as a regular web site. It will be gave new experience, best narrow cross-browser view, reduce processing time to receive browsing request, it's mean do not wasting your time to browsing codrate's web pages because it has been upgrade always modern coding ways. So, what do you waiting for ?. Try your own.

Copyright 2015 Pride - Company. Design by Esila