php生成xml,我在网上搜了有四种方法,但是当我自己做的时候结果不对,过程如下:
想要输出的xml是:
<?xml version="1.0" encoding="utf-8"?>  
<article>  
    <item>  
        <title size="1">title1</title>  
        <content>content1</content>  
        <pubdate>2009-10-11</pubdate>  
    </item>  
    <item>  
        <title size="1">title2</title>  
        <content>content2</content>  
        <pubdate>2009-11-11</pubdate>  
    </item>  
</article> 比如我使用SimpleXML创建XML文档,代码如下(都是网上的例子):
<?PHP  
$data_array = array(  
    array(  
    'title' => 'title1',  
    'content' => 'content1',  
        'pubdate' => '2009-10-11',  
    ),  
    array(  
    'title' => 'title2',  
    'content' => 'content2',  
    'pubdate' => '2009-11-11',  
    )  
);  
  
//  属性数组  
$attribute_array = array(  
    'title' => array(  
    'size' => 1  
    )  
);  
  
$string = <<<XML  
<?xml version='1.0' encoding='utf-8'?>  
<article>  
</article>  
XML;  
  
$xml = simplexml_load_string($string);  
  
foreach ($data_array as $data) {  
    $item = $xml->addChild('item');  
    if (is_array($data)) {  
        foreach ($data as $key => $row) {  
          $node = $item->addChild($key, $row);  
  
          if (isset($attribute_array[$key]) && is_array($attribute_array[$key]))  
            {  
              foreach ($attribute_array[$key] as $akey => $aval) {  
             //  设置属性值  
                  $node->addAttribute($akey, $aval);  
            }  
          }  
        }  
    }  
}  
echo $xml->asXML();  
?>   我是用txt文本创建的,然后保存成php格式(如1.php),可是网页打开是并没有生成要求的xml样子,而且保存的时候选择编码方式不一样结果也不一样。
求指导,大家具体是怎么写的,怎么保存的。php,xml