0

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.

entropy
  • 9
  • 1
  • 2
  • 1
    Why you bother to nicely indent the XML? Anyone can use a proper XML reader to format xml into nicely done hierarchy node. – ajreal Mar 09 '13 at 23:15
  • `$domtree->setIndent` is a lie. You just add a public property to the class that just carries a value. If you want to change the indentation, you can do afterwards with a regular expression as outlined here: [Converting indentation with preg_replace (no callback)](http://stackoverflow.com/questions/8616594/converting-indentation-with-preg-replace-no-callback) – hakre Mar 10 '13 at 09:42
  • thanx @harke, I had read that post before but again this works at a browser level but not on my actual xml file.. – entropy Mar 10 '13 at 09:58
  • just read ur second comment, so what you are saying is that there is not a way to do the formatting of my file the moment it is created and i have to process my file later with a regular expression. Am I correct? – entropy Mar 10 '13 at 10:01
  • @entropy: As far as changing the characters used for indentation is concerned, yes. Formatting the XML with indents of two spaces per each level is done by DomDocument (as outlined in the duplicate question). – hakre Mar 11 '13 at 07:40

1 Answers1

2

You should add

$domtree->formatOutput = true;

if you want it pretty printed. See formatOutput for details.

In one of the comments (Jochem Blok), it is suggested to turn preserveWhiteSpace off

$domtree->preserveWhiteSpace = false;
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198