Subject: | DATA_INDENT for character Data |
First of all thank you very much for that simple, but extremely useful
Module XML::Writer (in fact, XML::Writer inspired me to name my own
module XML::Reader).
However, I would like to draw to your attention a small inconvenience
in XML::Writer. I have a small program that exhibits the inconvenience
and I propose a simple patch to resolve that inconvenience.
Is there any chance that my patch could be incorporated into
XML::Writer ?
-- Klaus
Here is the small test program:
======
use XML::Writer;
my $writer = XML::Writer->new(OUTPUT => \*STDOUT, DATA_MODE => 1,
DATA_INDENT => 2, UNSAFE => 1);
$writer->xmlDecl("utf-8");
$writer->startTag("greeting", "class" => "simple");
$writer->characters("Hello, world!");
$writer->startTag("level-01");
$writer->startTag("level-02");
$writer->startTag("level-03");
$writer->characters("This is one text...");
$writer->characters("second text...");
$writer->startTag("level-04");
$writer->startTag("level-05");
$writer->characters("This is another text...");
$writer->endTag("level-05");
$writer->characters("Yet another text...");
$writer->endTag("level-04");
$writer->endTag("level-03");
$writer->endTag("level-02");
$writer->endTag("level-01");
$writer->endTag("greeting");
$writer->end();
======
The output of that small program is as follows:
======
<?xml version="1.0" encoding="utf-8"?>
<greeting class="simple">Hello, world!
<level-01>
<level-02>
<level-03>This is one text...second text...
<level-04>
<level-05>This is another text...</level-05>Yet another
text...
</level-04>
</level-03>
</level-02>
</level-01>
</greeting>
======
The indentation as such is perfect, however, what is inconvenient for
me is that character text does not automatically appear on its own
seperate line.
I allowed myself to patch the file "Writer.pm" in XML::Writer in the
following way -- look for the markers # begin by Klaus Eichner:
======
[...]
my $endTag = sub {
my $name = $_[0];
my $currentName = pop @elementStack;
$name = $currentName unless $name;
$elementLevel--;
# begin by Klaus Eichner, 17 July 2010
# if ($dataMode && $hasElement) {
if ($dataMode) {
# end by Klaus Eichner, 17 July 2010
$output->print("\n");
[...]
[...]
my $characters = sub {
my $data = $_[0];
if ($data =~ /[\&\<\>]/) {
$data =~ s/\&/\&\;/g;
$data =~ s/\</\<\;/g;
$data =~ s/\>/\>\;/g;
}
&{$escapeEncoding}($data);
# begin by Klaus Eichner, 17 July 2010
if ($dataMode) {
$output->print("\n");
$output->print(" " x ($elementLevel * $dataIndent));
}
# end by Klaus Eichner, 17 July 2010
$output->print($data);
[...]
======
Now the output of that small test program looks as follows (with
character text automatically placed on its own seperate line).
======
<?xml version="1.0" encoding="utf-8"?>
<greeting class="simple">
Hello, world!
<level-01>
<level-02>
<level-03>
This is one text...
second text...
<level-04>
<level-05>
This is another text...
</level-05>
Yet another text...
</level-04>
</level-03>
</level-02>
</level-01>
</greeting>
======