用PHP格式化打印XML文件中的XML输出
我试图在我的根文件夹中输出一个xml文件。文件已创建,但所有元素及其内容都保存在一条直线上,如下所示:用PHP格式化打印XML文件中的XML输出,php,xml,format,output,Php,Xml,Format,Output,我试图在我的根文件夹中输出一个xml文件。文件已创建,但所有元素及其内容都保存在一条直线上,如下所示: *<?xml version="1.0" encoding="UTF-8"?> <letterA> <lemma> <word>word1</word> <textfiles>title of file</textfiles> <videofiles>video.mp3</
*<?xml version="1.0" encoding="UTF-8"?> <letterA> <lemma> <word>word1</word> <textfiles>title of file</textfiles> <videofiles>video.mp3</videofiles> </lemma>
<?xml version="1.0" encoding="UTF-8"?>
<letterA>
<lemma>
<word>song1.mp3</word>
<textfiles>title of file</textfiles>
<videofiles>video.mp3</videofiles>
</lemma>
</letterA>
*video.mp3文件的word1标题
*
但我想要这样的东西:
*<?xml version="1.0" encoding="UTF-8"?> <letterA> <lemma> <word>word1</word> <textfiles>title of file</textfiles> <videofiles>video.mp3</videofiles> </lemma>
<?xml version="1.0" encoding="UTF-8"?>
<letterA>
<lemma>
<word>song1.mp3</word>
<textfiles>title of file</textfiles>
<videofiles>video.mp3</videofiles>
</lemma>
</letterA>
歌曲1.mp3
文件标题
视频.mp3
这是我的密码:
<?php
/* create a dom document with encoding utf8 */
$domtree = new DOMDocument('1.0', 'UTF-8');
$domtree->preserveWhiteSpace = true;
$domtree->setIndent = (1);
/* $domtree->formatOutput = (true); */
/* create the root element of the xml tree */
$letterARoot = $domtree->createElement("letterA");
/* append it to the document created */
$letterARoot = $domtree->appendChild($letterARoot);
$alemma = $domtree->createElement("lemma");
$alemma = $letterARoot->appendChild($alemma);
/* you should enclose the following two lines in a cicle */
$alemma->appendChild($domtree->createElement('word','word1));
$alemma->appendChild($domtree->createElement('textfiles','title of file'));
$alemma->appendChild($domtree->createElement('videofiles','video.mp3'));
/* save the file in my root folder */
$domtree->save("firstFile.xml") or die("Error");
/* get the xml printed */
/* echo $domtree->saveXML(); */
?>
createElement(“letter A”);
/*将其附加到创建的文档中*/
$letterARoot=$domtree->appendChild($letterARoot);
$alemma=$domtree->createElement(“引理”);
$alemma=$letterARoot->appendChild($alemma);
/*你应该把下面两行用圆圈括起来*/
$alemma->appendChild($domtree->createElement('word','word1));
$alemma->appendChild($domtree->createElement('textfiles','title of file');
$alemma->appendChild($domtree->createElement('videofiles','video.mp3');
/*将文件保存在我的根文件夹中*/
$domtree->save(“firstFile.xml”)或die(“Error”);
/*打印xml*/
/*echo$domtree->saveXML()*/
?>
如果有人能帮忙,我将不胜感激。正如您所看到的,我使用了“preservewitspace”和“setIndent”,但效果不好。我也尝试过再次使用“formatOutput”不好。我担心的不是我的xml在浏览器上的外观,而是它在实际文件(即上面示例中的firstFile.xml)中的外观。
thanx。您应该添加
$domtree->formatOutput = true;
如果你想要漂亮的印刷品。有关详细信息,请参阅
在其中一条注释()中,建议关闭preserveWhiteSpace
$domtree->preserveWhiteSpace = false;
为什么要费心精确地缩进XML?任何人都可以使用适当的XML读取器将XML格式化为完成得很好的层次结构节点。
$domtree->setIndent
是个谎言。您只需将一个公共属性添加到只包含值的类中。如果你想改变缩进,你可以在后面用一个正则表达式,如下所述:thanx@harke,我以前读过那篇文章,但这同样适用于浏览器级别,但不适用于我的实际xml文件。请阅读你的第二条评论,因此,您的意思是,在创建文件的那一刻,没有一种方法可以对文件进行格式化,我必须稍后使用正则表达式处理文件。我说得对吗?@entropy:就更改缩进字符而言,是的。DomDocument(如重复问题中所述)将XML格式设置为每层缩进两个空格。thanx但我已经尝试了所有这些,它在浏览器上运行良好,但在我保存的文件中不起作用。。。还有其他想法吗?@没有,对不起。我测试了你的脚本,添加formatOutput=true
修复了我计算机上的问题。