PHP XMLWriter输出格式进行测试

时间:2018-09-30 09:27:55

标签: php xmlwriter

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

是否可以在每个元素的末尾添加换行符?

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

print (chr(13).chr(10)); 

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

$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');                                     

这是输出

<?xml version="1.0" encoding="UTF-8"?><vehicleServiceRequest><vehicleDetails><companyIdentifier>001</companyIdentifier><transportCompanyName>Mixed Freight Services</transportCompanyName><dispatchNumber>174672</dispatchNumber>

1 个答案:

答案 0 :(得分:0)

我知道这是一个旧线程,但是由于没有看到有用的答复,因此下面是缺少正确缩进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