XML beautify using perl XML::Writer

There are code that we generate somehow having the whole data
merged into just ONE long line.

And end-user complained can’t be processed.
Which is not so easy to read.

unless you did some magic before viewing as follows :

xmllint --format IDM26004.xml |more

which did reformatting it.. into a human readable tab/indent.

However, later I found out the fix for generating script is quite easy.
Just need to add the following item.

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

which is decribed here :

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.
DATA_INDENT
 
    A numeric value or white space; if this parameter is present, it represents the indent step for elements in data mode (it will be ignored when not in data mode). If it is white space it will be repeated for each level of indentation.

so the full look like follows..

# =====================================================================
# gen_xml () 
# ---------------------------------------------------------------------
 
sub gen_xml {
 
  my ($next_time) = @_;
  my @nowtime = localtime (time());
 
  syslog ("info", "Generating Tide XML");
 
  my $xml;
 
  my $writer = new XML::Writer (
    OUTPUT => \$xml, DATA_MODE => 1,DATA_INDENT => 2
  );
  $writer->xmlDecl();
  $writer->startTag ("tides");
  $writer->emptyTag ("timezone",
    'name' => 'MST', value=>'8.0'
  );
 
..

so result would as :

[[email protected] tmp]$ head IDM26004.xml 



  
  Saturday 22/01/11
  
    
      
      
      

References :

http://search.cpan.org/~josephw/XML-Writer-0.612/Writer.pm

Related Post

3 Responses

  1. casiemec says:

    This rather good idea is necessary just by the way

    Reply
  2. TheN2S says:

    I was wondering if I can use this for my wordpress 2.9.2 site. I simply want to mirror/sync/backup all my newly UPLOADED files to a new BACKUP website. (I don’t want to wait for the cron job to kick in)

    Reply
  3. Madelyn Monoz says:

    I love what you guys are up too. Such clever work and exposure! Keep up the good works guys I’ve added you guys to blogroll.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *