Home  >  Article  >  Backend Development  >  php生成xml文档有关问题

php生成xml文档有关问题

WBOY
WBOYOriginal
2016-06-13 11:10:44641browse

php生成xml文档问题
php生成xml,我在网上搜了有四种方法,但是当我自己做的时候结果不对,过程如下:
想要输出的xml是:
  

  
      
        title1  
        content1  
        2009-10-11  
    
  
      
        title2  
        content2  
        2009-11-11  
    
  
 

比如我使用SimpleXML创建XML文档,代码如下(都是网上的例子):

<?PHP   <br />$data_array = array(  <br>    array(  <br>    'title' => 'title1',  <br>    'content' => 'content1',  <br>        'pubdate' => '2009-10-11',  <br>    ),  <br>    array(  <br>    'title' => 'title2',  <br>    'content' => 'content2',  <br>    'pubdate' => '2009-11-11',  <br>    )  <br>);  <br>  <br>//  属性数组  <br>$attribute_array = array(  <br>    'title' => array(  <br>    'size' => 1  <br>    )  <br>);  <br>  <br>$string = <?xml  version='1.0' encoding='utf-8'?>  <br><article>  <br></article>  <br>XML;  <br>  <br>$xml = simplexml_load_string($string);  <br>  <br>foreach ($data_array as $data) {  <br>    $item = $xml->addChild('item');  <br>    if (is_array($data)) {  <br>        foreach ($data as $key => $row) {  <br>          $node = $item->addChild($key, $row);  <br>  <br>          if (isset($attribute_array[$key]) && is_array($attribute_array[$key]))  <br>            {  <br>              foreach ($attribute_array[$key] as $akey => $aval) {  <br>             //  设置属性值  <br>                  $node->addAttribute($akey, $aval);  <br>            }  <br>          }  <br>        }  <div class="clear">
                 
              
              
        
            </div>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn