用于测试的 PHP XMLWriter 输出格式

Mal*_*lmf 2 php xmlwriter

XML 消息的接收者要求我在每个元素的末尾添加换行符。他们之所以提出这一要求,是因为消息直接进入 Oracle 数据库,并且他们只能将其视为一个长字符串,这使得测试变得困难。他们当前的解决方法是在 Internet Explorer 中打开它,但这需要时间

有没有办法在每个元素的末尾添加换行符?

我尝试在每个元素的末尾添加以下内容

print (chr(13).chr(10)); 
Run Code Online (Sandbox Code Playgroud)

但这对输出没有影响。这是我的消息的开始

$writer = new XMLWriter();  
$writer->openURI('php://output');
$writer->startDocument('1.0','UTF-8');   
$writer->setIndent(TRUE);   
    $writer->startElement('vehicleServiceRequest'); 
        $writer->startElement('vehicleDetails');
            $writer->writeElement('companyIdentifier', $AIS_transmission_code);
            $writer->writeElement('transportCompanyName', $user['company_name']);
            $writer->writeElement('dispatchNumber', $booking_reference);                            
            $writer->writeElement('plannedDispatchDate', date("Y-m-dTH:i:s"));                      // We use today as the planned dispatch date
            $writer->writeElement('dispatchPostCode', $user['postcode']);                           
            $writer->writeElement('dispatchCountryCode', 'UK');                                     
Run Code Online (Sandbox Code Playgroud)

这是输出

<?xml version="1.0" encoding="UTF-8"?><vehicleServiceRequest><vehicleDetails><companyIdentifier>001</companyIdentifier><transportCompanyName>Mixed Freight Services</transportCompanyName><dispatchNumber>174672</dispatchNumber>
Run Code Online (Sandbox Code Playgroud)

小智 5

我知道这是一个旧线程,但由于我没有看到任何有用的回复,因此下面是您缺少正确缩进 XML 文件的代码行。

$writer->setIndent(TRUE); // after your line of code add the below line
$writer->setIndentString('    '); // here you just have to add the string you wish to use for indentation; in this case I used 4 spaces
Run Code Online (Sandbox Code Playgroud)