如何使用PHP创建XML

时间:2013-04-26 04:52:29

标签: php xml

我是PHP的初学者。我想动态创建一个XML文件,我知道如何做到这一点。但在这种情况下,在XML文件中,一个节点应包含属性“name”,其值为$_POST变量。如何编写PHP代码来创建包含属性“name”的节点的XML文件。

2 个答案:

答案 0 :(得分:0)

它可能对您有所帮助:

<?php
// Send the headers
header('Content-type: text/xml');
header('Pragma: public');
header('Cache-control: private');
header('Expires: -1');
echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>";

echo '<xml>';

// echo some dynamically generated content here


echo '</xml>';

?>

最后使用.php扩展名

保存文件

答案 1 :(得分:0)

 As a solution to your problem please try executing following example code snippet 

The below code snippet is created on basis of assumption that there is a form for user registration containing fields such as email ,address depicting the procedure for dynamically generation of **xml** content using php

 <?php
     $xmlstring='<xml>';
    if($_SERVER['REQUEST_METHOD']=='POST')
    {
      $xmlstring.='<user>
     <email>'.$POST['email'].'</email>
     <address>'.$_POST['address'].'</address>
     </user>';
    }
    $xmlstring.='</xml>';
    header('Content-type:text/xml');
    echo $xmlstring;
    die;
?>