I'm trying to add newlines to each <>
line in the XML output of XML::LibXML
from https://stackoverflow.com/a/2934794/19508169.
use strict;
use warnings;
use XML::LibXML;
my $doc = XML::LibXML::Document->new('1.0', 'utf-8');
my $root = $doc->createElement('my-root-element');
$root->setAttribute('some-attr'=> 'some-value');
my %elements = (
color => 'blue',
metal => 'steel',
);
for my $name (keys %elements) {
my $tag = $doc->createElement($name);
my $value = $elements{$name};
$tag->appendTextNode($value);
$root->appendChild($tag);
}
$doc->setDocumentElement($root);
print $doc->toString();
But, when I tried this code, I got the result without newlines:
%> perl test2.pl
<?xml version="1.0" encoding="utf-8"?>
<my-root-element some-attr="some-value"><color>blue</color><metal>steel</metal></my-root-element>
I expected the below:
<?xml version="1.0" encoding="utf-8"?>
<my-root-element some-attr="some-value">
<color>blue</color>
<metal>steel</metal>
</my-root-element>
How to add newlines for each XML output?
If add " \n"
into print $doc->toString();
as print $doc->toString(), " \n";
, it does not work.
The toString()
method (documented in XML::LibXML::Document has an optional numeric argument that changes how the output is formatted.
toString
$docstring = $dom->toString($format);
toString is a DOM serializing function, so the DOM Tree is serialized into an XML string, ready for output.
IMPORTANT: unlike toString for other nodes, on document nodes this function returns the XML as a byte string in the original encoding of the document (see the actualEncoding() method)! This means you can simply do:
open my $out_fh, '>', $file; print {$out_fh} $doc->toString;
regardless of the actual encoding of the document. See the section on encodings in XML::LibXML for more details.
The optional $format parameter sets the indenting of the output. This parameter is expected to be an integer value, that specifies that indentation should be used. The format parameter can have three different values if it is used:
If $format is 0, than the document is dumped as it was originally parsed
If $format is 1, libxml2 will add ignorable white spaces, so the nodes content is easier to read. Existing text nodes will not be altered
If $format is 2 (or higher), libxml2 will act as $format == 1 but it add a leading and a trailing line break to each text node.
libxml2 uses a hard-coded indentation of 2 space characters per indentation level. This value can not be altered on run-time.
The value "1" gives the results you want.
print $doc->toString(1);
Output:
<?xml version="1.0" encoding="utf-8"?>
<my-root-element some-attr="some-value">
<metal>steel</metal>
<color>blue</color>
</my-root-element>