如何在XMLWriter php文档中添加额外的空格/行

时间:2017-06-16 08:59:54

标签: php xmlwriter

我在php中使用XMLWriter来创建文档,但我需要通过在元素和组件之间添加更多的行和空格来格式化它。我该怎么做。请看下面的例子

PHP代码:

<?php
/*
 * PHP XMLWriter - How to create a simple xml
 */

//create a new xmlwriter object
$xml = new XMLWriter(); 
//using memory for string output
$xml->openMemory(); 
//set the indentation to true (if false all the xml will be written on one line)
$xml->setIndent(true);
//create the document tag, you can specify the version and encoding here
$xml->startDocument(); 
//Create an element
$xml->startElement("Customer"); 
//Write to the element
$xml->writeElement("id", "1");
$xml->writeElement("name", "Oluwafemi"); 
$xml->writeElement("address", "Cresent Drive, TX");
$xml->endElement(); //End the element
//output the xml (obviosly this output could be written to a file)
echo htmlentities($xml->outputMemory()); 
?>

输出是:

<?xml version="1.0"?>
<Customer>
 <id>1</id>
 <name>Oluwafemi</name>
 <address>Cresent Drive, TX</address>
</Customer>

但我需要生成如下的输出。

<?xml version="1.0"?>

<Customer>

     <id>1</id>
     <name>Oluwafemi</name>
     <address>Cresent Drive, TX</address>

</Customer>

1 个答案:

答案 0 :(得分:0)

与评论所说的不同, IS 非常受支持,对于您的特定情况,只需使用:

$xml->setIndentString("     ");