I am trying to output an xml file in my root folder. The file is created but all the elements and their contents are saved in one straight line like this:
*<?xml version="1.0" encoding="UTF-8"?> <letterA> <lemma> <word>word1</word> <textfiles>title of file</textfiles> <videofiles>video.mp3</videofiles> </lemma>
* But I want something like this:
<?xml version="1.0" encoding="UTF-8"?>
<letterA>
<lemma>
<word>song1.mp3</word>
<textfiles>title of file</textfiles>
<videofiles>video.mp3</videofiles>
</lemma>
</letterA>
Here is my code:
<?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(); */
?>
I would appreciate it if someone could help. As you can see I have used 'preserveWhiteSpace' with 'setIndent' but no good. I have also tried using 'formatOutput' no good again. It is not the appearance of my xml on the browser I am worried about but its appearance in the actual file (namely the firstFile.xml in my example above). thanx in advance.