indented xml using XML::DOM

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
hey all

  I am using XML::Dom->printToFile to write out an XML::Dom document
that I created. Is there a way to indent the output.  By default the
whole xml is written out in just one line.

  XML::Handler::XMLWriter with newlines option claims to do this but
doesn't take an XML::Dom object.


Re: indented xml using XML::DOM

Parapura Rajkumar wrote:

Quoted text here. Click to load it

XML::DOM doesn't seem to provide any mean of pretty printing the output.
Note that in a purely XML context, there is no need for it, and adding
whitespace indiscriminately could even lead to the output becoming invalid (wrt
a DTD).

That said you can always post-process your XML, using xmllint (which comes with
libxml2) or xml_pp (which comes with XML::Twig) for example.

Finally, I have to add a word of caution about XML::DOM: these days, XML::LibXML
is the preferred DOM implementation in Perl. It is a lot more powerful than
XML::DOM, and more actively maintained AFAIK. It is also quite compatible with
XML::DOM, so it is pretty easy to port the code.


Site Timeline