PHP格式的XML输出
我正在生成一些XML数据以响应HTTP POST请求。我正在设置哑剧PHP格式的XML输出,php,xml,Php,Xml,我正在生成一些XML数据以响应HTTP POST请求。我正在设置哑剧 header('Content-type: text/xml'); 到目前为止,一切进展顺利 我将生成xml响应,如下所示: $response = '<?xml version="1.0" encoding="utf-8"?>'; $response .= '<error>'; $response .= '<description>'.$error.'</descriptio
header('Content-type: text/xml');
到目前为止,一切进展顺利
我将生成xml响应,如下所示:
$response = '<?xml version="1.0" encoding="utf-8"?>';
$response .= '<error>';
$response .= '<description>'.$error.'</description>';
$response .= '</error>';
echo $response;
$response='';
$response.='';
$response.=''.$error';
$response.='';
回音$应答;
我现在希望XML的格式是这样的,而不是看到这样的响应:
<?xml version="1.0" encoding="utf-8"?><error><description>Login Error: No Username Supplied</description></error>
<?xml version="1.0" encoding="utf-8"?>
<error>
<description>Login Error: No Username Supplied</description>
</error>
登录错误:未提供用户名
他们看到这样的反应:
<?xml version="1.0" encoding="utf-8"?><error><description>Login Error: No Username Supplied</description></error>
<?xml version="1.0" encoding="utf-8"?>
<error>
<description>Login Error: No Username Supplied</description>
</error>
登录错误:没有提供用户名
我以前没有用PHP处理过XML输出,所以不确定是否有内置方法在输出上执行“漂亮打印”类型的函数?使用DOM库,并将
formatOutput
属性设置为true
$doc = new DOMDocument('1.0', 'utf-8');
$doc->formatOutput = true;
$root = $doc->createElement('error');
$doc->appendChild($root);
$desc = $doc->createElement('description', $error);
$root->appendChild($desc);
echo $doc->saveXML();
演示~使用DOM库并将
formatOutput
属性设置为true
$doc = new DOMDocument('1.0', 'utf-8');
$doc->formatOutput = true;
$root = $doc->createElement('error');
$doc->appendChild($root);
$desc = $doc->createElement('description', $error);
$root->appendChild($desc);
echo $doc->saveXML();
演示~如何输出XML?看一看这个线程,.稍微偏离主题:“正确格式化XML”-两个版本都比另一个版本本身更正确。你的意思可能是“更具可读性”。然后文档定义必须允许这样做(即可以跳过空白)。您如何输出XML?看一看这个线程,.稍微偏离主题:“正确格式化XML”-两个版本都比另一个版本本身更正确。你的意思可能是“更具可读性”。然后文档定义必须允许这样做(即可以跳过空白)。