我怎样才能显示空白格式的XML? [英] How can I display XML with whitespace formatting?

查看:165
本文介绍了我怎样才能显示空白格式的XML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网页,从现有的XML应用更改构建一个XML。我想在一个 textarea 中输出新的XML文件作为预览。它显示原始XML中存在的任何节点,其中原始XML恰好有适当的空格/格式(缩进和换行符),但任何新节点都显示在一行中,没有缩进。例子:

 <原始父节点> 
< original child>值< / original child>
< /原始父节点>
<原始父节点>新孩子>值< /新孩子><新孩子>值< /新孩子><新孩子>值< /新孩子><新孩子>值<新孩子>值< /新孩子><新孩子>值< /新孩子>
< /原始父节点>

以下是在XML中写入和回读的代码:

  $ dom = new DOMDocument('1.0'); 
$ dom-> preserveWhiteSpace = true;
$ dom-> formatOutput = true;
$ dom-> loadXML($ xml-> asXML());
file_put_contents($ file,$ dom-> saveXML());
echo< textarea cols ='100'rows ='40'>file_get_contents($ file)。< / textarea>;

我也使用SimpleXML来操作XMLS。我怎样才能得到正确的空白显示新的节点?

解决方案

我发现 formatOutput 仅在 preserveWhiteSpace 被禁用时才起作用:

  $ dom = new DOMDocument; 
$ dom-> preserveWhiteSpace = false;
$ dom-> formatOutput = true;
$ dom-> loadXML($ xml);
echo $ dom-> saveXML();


I have a web page that builds an XML from an existing XML applying changes. I want to output the new XML file in a textarea as a preview. It displays any nodes that were present in the original XML with the proper whitespaces/formatting (indents and linebreaks) that the original XML had just fine, but any new nodes are all displayed on one line with no indents. Example:

<original parent node>
    <original child>value</original child>
</original parent node>
<original parent node>
    <new child>value</new child><new child>value</new child><new child>value</new child><new child>value</new child><new child>value</new child><new child>value</new child>
</original parent node>

Here is the code that writes and reads back in the XML:

$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = true;
$dom->formatOutput = true;
$dom->loadXML($xml->asXML());
file_put_contents($file, $dom->saveXML());
echo "<textarea cols='100' rows='40'>".file_get_contents($file)."</textarea>";

I'm also using SimpleXML to manipulate the XMLS. How can I get the proper whitespacing to display for the new nodes?

解决方案

I've found that formatOutput only works when preserveWhiteSpace is disabled:

$dom = new DOMDocument;
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($xml);
echo $dom->saveXML();

这篇关于我怎样才能显示空白格式的XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆