http://www.perlmonks.org?node_id=952674

gdanenb has asked for the wisdom of the Perl Monks concerning the following question:

For some reason when I append element to xml file, it's written in one line, i.e. not formatted

Original xml:

<configuration> <property> <name>test1</name> </property> </configuration>
And the code is:
my $parser =XML::LibXML->new(); my $doc =$parser->parse_file($file) or die $!; my $root =$doc->getDocumentElement; my $searchPath="/configuration"; my ($val)=$root->findnodes($searchPath); my $propTag=$doc->createElement("property"); $val->appendChild($propTag); my $nameTag=$doc->createElement("name"); $nameTag->appendTextNode($name); $propTag->appendChild($nameTag); $doc->setDocumentElement($root); $doc->toFile($file,1);
Which resulted with:
<configuration> <property> <name>test1</name> </property> <property><name>test2</name></property></configuration>
instead of:
<configuration> <property> <name>test1</name> </property> <property> <name>test2</name> </property> </configuration>