【问题标题】:save XML file in minified form using DOMDocument使用 DOMDocument 以缩小形式保存 XML 文件
【发布时间】: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);

【问题讨论】:

  • 您的示例中的 preserveWhiteSpaceformatOutput 应该注意这一点:3v4l.org/BcHb8
  • 是的 formatOutput 工作我做错了谢谢你

标签: php html xml


【解决方案1】:

试试这个,你必须使用saveXML()而不是save(),

<?php

$xml = new DOMDocument('1.0');

$xml->preserveWhiteSpace = false; 
$xml->formatOutput = false;

$root = $xml->createElement('book');
$root = $xml->appendChild($root);

$title = $xml->createElement('title');
$title = $root->appendChild($title);

$text = $xml->createTextNode("This is the \n title");
$text = $title->appendChild($text);

echo "Saving all the document:\n";

$xml_content = $xml->saveXML();
echo $xml_content . "\n";

$xml_content = str_replace(array(">\n", ">\t"), '>', trim($xml_content, "\n"));

echo $xml_content . "\n";

// Write the contents back to the file
$filename = "/tmp/xml_minified.xml";
file_put_contents($filename, $xml_content);
?>

【讨论】:

  • 它不会帮助我使用 DOMDocument 来创建和保存文件
  • 这也会从文档中的文本节点和属性中去除任何空白,感觉有点冒险。
  • @iainn 看起来你没有看到我没有替换任何空格,而是 array("\n", "\t")
  • @iainn 还纠正了替换 \n 和 \t 的代码,通过执行检查。
  • 为什么将preserveWhiteSpaceformatOutput 设置为false?您的评论甚至说我们想要一个好的输出,而这与我们想要的相反。原贴还说昨天问题解决了。使用savesaveXML 对输出也没有影响,只是保存到文件而不是字符串。
【解决方案2】:

您已经在使用正确的选项。 DOMDocument::$formatOutputDOMDocument::$preserveWhiteSpace:

格式化输出

DOMDocument::$formatOutput 将缩进空白节点添加到 XML DOM(如果已保存)。 (默认禁用。)

$document = new DOMDocument();
$body = $document->appendChild($document->createElement('body'));
$div = $body->appendChild($document->createElement('div'));
$div
  ->appendChild($document->createElement('p'))
  ->appendChild($document->createTextNode('hello'));

echo "Not Formatted:\n", $document->saveXML();

$document->formatOutput = TRUE;
echo "\nFormatted:\n", $document->saveXML();

输出:

Not Formatted: 
<?xml version="1.0"?> 
<body><div><p>hello</p></div></body> 

Formatted: 
<?xml version="1.0"?> 
<body>
  <div>
    <p>hello</p>
  </div> 
</body>

但是,如果这里是文本子节点,它不会缩进。它试图避免更改 HTML/XML 文档的文本输出。所以它通常不会用现有的缩进空白节点重新格式化加载的文档。

保留空白

DOMDocument::$preserveWhiteSpace 是解析器的一个选项。如果禁用(默认启用),解析器将忽略任何仅包含空格的文本节点。缩进是带有换行符和一些空格或制表符的文本节点。它可用于从 XML 中删除缩进。

$xml = <<<'XML'
<?xml version="1.0"?> 
<body>
  <div>
    <p>hello</p>
  </div> 
</body>
XML;

$document = new DOMDocument();
$document->preserveWhiteSpace = FALSE;
$document->loadXML($xml);
echo $document->saveXML();

输出:

<?xml version="1.0"?> 
<body><div><p>hello</p></div></body>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-03
    • 1970-01-01
    • 1970-01-01
    • 2011-10-19
    • 1970-01-01
    • 2012-02-17
    相关资源
    最近更新 更多