0
拖尾的回报,我从生成数据库查询一些XML如下:PHP - 在生成的XML
$doc = new DOMDocument('1.0', 'utf-8');
$doc->formatOutput = true;
$contactsElement = $doc->createElement('contacts');
$doc->appendChild($contactsElement);
foreach($contacts as $contact){
$firstName = $contact->getField('first') ;
$lastName = $contact->getField('last') ;
$contactElement = $doc->createElement('contact');
$firstNameElement = $doc->createElement('first_name', $firstName);
$lastNameElement = $doc->createElement('last_name', $lastName);
$contactElement->appendChild($firstNameElement);
$contactElement->appendChild($lastNameElement);
$contactsElement->appendChild($contactElement);
}
echo $doc->saveXML();
这是工作良好,但它产生在年底闭幕后的附加导线。这里有一个例子:
<?xml version="1.0" encoding="utf-8"?>
<contacts>
<contact>
<first_name>Penny</first_name>
<last_name>Lancaster</last_name>
</contact>
<contact>
<first_name>Geoff</first_name>
<last_name>McDermott</last_name>
</contact>
</contacts>
// blank line is here
我看不出有什么可能会造成这个,或者如果有一个语法的变化,我需要做 - 至于如何防止尾随空白行出现在生成的XML有什么建议?
你有一个结束'>'在你的文件 - 标签?如果是这样,请将其删除 –
我确实有一个结束?>标记 - 已将其删除,但仍然会得到与XML末尾的空行相同的结果。 – user982124
改为将我的修复程序添加为答案。 –