0

我不知道我的错误来自哪里,我是 xml 新手,我需要一些帮助。我想使用 php 编写一个 xml 文件。我有以下 php 代码:

<?php 
 $xml = array(); 
 $xml [] = array( 
 'error' => 'ERROR_ID'
  ); 

 $doc = new DOMDocument(); 
 $doc->formatOutput = true; 

 $r = $doc->createElement( "xml" ); 
 $doc->appendChild( $r ); 

 $parameters = $doc->createElement( "parameters" );    
  $error = $doc->createElement( "error" ); 
 $error->appendChild( 
 $doc->createTextNode( $xml['error'] ) 
 ); 
 $parameters->appendChild( $error ); 


 $r->appendChild( $parameters ); 


 echo $doc->saveXML(); 
 $doc->save("write.xml") 
 ?>

我的 xml 文件应如下所示:

<?xml version="1.0"?>
<xml>
  <parameters>
    <error>ERROR_ID</error>
  </parameters>
</xml>

但是文件中没有显示 ERROR_ID 文本节点。出了什么问题?

4

1 回答 1

4

第一个选项:替换这一行:

$doc->createTextNode( $xml['error'] ) 

有了这个:

$doc->createTextNode( $xml [0] ['error'] ) 

第二种选择:替换这一行:

$xml [] = array( 

有了这个:(并删除第一行)

$xml = array( 
于 2012-05-10T19:37:50.233 回答