【发布时间】:2019-02-14 19:41:07
【问题描述】:
我想通过以缩小形式保存 xml 文件来节省空间
例如
<body>
<div>
<p>hello</p>
<div/>
</div>
</body>
应该这样保存
<body><div><p>hello</p><div/></div></body>
我正在使用 DOMDocument 来创建这样的 xml 文件
$xml = new DOMDocument("1.0", "UTF-8");
$xml->preserveWhiteSpace = false;
$xml->formatOutput = false;
$feed = $xml->createElement("feed");
$feed = $xml->appendChild($feed);
/*add attribute*/
$feed_attribute = $xml->createAttribute('xmlns:xsi');
$feed_attribute->value = 'http://www.w3.org/2001/XMLSchema-instance';
$feed->appendChild($feed_attribute);
$aggregator = $xml->createElement("aggregator");
$aggregator = $feed->appendChild($aggregator);
$name = $xml->createElement('name', 'test.com');
$aggregator->appendChild($name);
...etc
$xml->save(public_path() .$string, LIBXML_NOEMPTYTAG);
【问题讨论】:
-
您的示例中的
preserveWhiteSpace和formatOutput应该注意这一点:3v4l.org/BcHb8 -
是的 formatOutput 工作我做错了谢谢你