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.

5
  • 1
    Why you bother to nicely indent the XML? Anyone can use a proper XML reader to format xml into nicely done hierarchy node. Commented Mar 9, 2013 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) Commented Mar 10, 2013 at 9:42
  • thanx @harke, I had read that post before but again this works at a browser level but not on my actual xml file.. Commented Mar 10, 2013 at 9: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? Commented Mar 10, 2013 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). Commented Mar 11, 2013 at 7:40

1 Answer 1

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;
Sign up to request clarification or add additional context in comments.

2 Comments

thanx but I've tried all that and it works fine on the browser but not in my saved file... Any other ideas?
@entropy No, sorry. I tested your script and adding formatOutput = true fixes the problem on my computer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.