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

dear monks, after having found a lot of helpful information around the monastery, i just decided to join in here.
well, there is one thing i just came across and i'm really not sure if this is meant to be like this or if i am doing something wrong.

what i try to do is to write out some (also) human readable xml. this happens while i process/parse a stream of data, so i don't want to build up the whole xml structure (in a hash) and write it all out at once but do writes sequentially while data portions are processed.
to me, XML::Writer seems jsut to offer what i'm looking for.
since the output should be human readable and not come out as one long single line, i use the 'NEWLINES' arg on creation of my XML::Writer object.
somehow, the output is not quite as i expected; here is some sample:
use strict; use XML::Writer; use IO::File; my $output = new IO::File(">testout.xml"); my $writer = new XML::Writer(OUTPUT => $output, NEWLINES => 1); $writer->xmlDecl(); $writer->startTag("rootnode", "xmlns" => "my:name:space", "version" => + "1", "creator" => "anti"); $writer->emptyTag("emptynode", "value" => "abc"); $writer->startTag("node", "name" => "foo"); $writer->characters("Hello, world!"); $writer->endTag("node"); $writer->endTag("rootnode"); $writer->end(); $output->close();
in the putput-file (testout.xml) i find after running the above:
<?xml version="1.0"?> <rootnode xmlns="my:name:space" version="1" creator="anti" ><emptynode value="abc" /><node name="foo" >Hello, world!</node ></rootnode >
but i would prefer this to look like:
<?xml version="1.0"?> <rootnode xmlns="my:name:space" version="1" creator="anti"> <emptynode value="abc" /> <node name="foo"> Hello, world! </node> </rootnode>
indenting is useful but not as important as getting the lines wrapped nicely.
any ideas or suggestions?

thanks a lot in advance -- greetings, anti

edit: i'm using XML::Writer version 0.604; should be the latest...

Replies are listed 'Best First'.
Re: newline behaviour with XML::Writer
by psini (Deacon) on Sep 13, 2008 at 20:48 UTC

    Try creating the object with:

    my $writer = new XML::Writer(OUTPUT => $output, DATA_MODE => 1, DATA_INDENT=>2);

    As per XML::Writer POD the behavior of NEWLINES is exactly what you get, but DATA_MODE should do what you want:

    DATA_MODE A true or false value; if this parameter is present and its value is true, then the module will enter a special data mode, inserting newlines automatically around elements and (unless UNSAFE is also specified) reporting an error if any element has both characters and elements as content.

    Rule One: "Do not act incautiously when confronting a little bald wrinkly smiling man."

      thanks a lot for pointing that out!
      i tried to use DATA_MODE together with NEWLINES which did not help but what you pointed out works correct and is plain simple. ... stupid me ... guess it's still along way to enlightenent for me ;)

      i'm still wondering which use the newlines parameter has, what use is the NEWLINES garbeled output?

        Dunno. My guess is that it is to obtain the shortest possible lines without inserting newlines within data. OTOH breaking a tag does not change the XML content.

        Rule One: "Do not act incautiously when confronting a little bald wrinkly smiling man."