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

Get Free Email Updates!

Signup now and receive an email once I publish new content.

I will never give away, trade or sell your email address. You can unsubscribe at any time.

Like

Related Post

2 Responses

  1. namran says:

    4) Module parameters.
    ———————
    The following module parameters can be passed:

    mode=

    Possible values are 0 (round robin policy, default) and 1 (active backup
    policy), and 2 (XOR). See question 9 and the HA section for additional info.

    miimon=

    Use integer value for the frequency (in ms) of MII link monitoring. Zero value
    is default and means the link monitoring will be disabled. A good value is 100
    if you wish to use link monitoring. See HA section for additional info.

    downdelay=

    Use integer value for delaying disabling a link by this number (in ms) after
    the link failure has been detected. Must be a multiple of miimon. Default
    value is zero. See HA section for additional info.

    updelay=

    Use integer value for delaying enabling a link by this number (in ms) after
    the “link up” status has been detected. Must be a multiple of miimon. Default
    value is zero. See HA section for additional info.

    arp_interval=

    Use integer value for the frequency (in ms) of arp monitoring. Zero value
    is default and means the arp monitoring will be disabled. See HA section
    for additional info. This field is value in active_backup mode only.

    arp_ip_target=

    An ip address to use when arp_interval is > 0. This is the target of the
    arp request sent to determine the health of the link to the target.
    Specify this value in ddd.ddd.ddd.ddd format.

    If you need to configure several bonding devices, the driver must be loaded
    several times. I.e. for two bonding devices, your /etc/conf.modules must look
    like this:

    alias bond0 bonding
    alias bond1 bonding

    options bond0 miimon=100
    options bond1 -o bonding1 miimon=100

    Reply
  2. namran says:

    4) Module parameters.
    ———————
    The following module parameters can be passed:

    mode=

    Possible values are 0 (round robin policy, default) and 1 (active backup
    policy), and 2 (XOR). See question 9 and the HA section for additional info.

    miimon=

    Use integer value for the frequency (in ms) of MII link monitoring. Zero value
    is default and means the link monitoring will be disabled. A good value is 100
    if you wish to use link monitoring. See HA section for additional info.

    downdelay=

    Use integer value for delaying disabling a link by this number (in ms) after
    the link failure has been detected. Must be a multiple of miimon. Default
    value is zero. See HA section for additional info.

    updelay=

    Use integer value for delaying enabling a link by this number (in ms) after
    the “link up” status has been detected. Must be a multiple of miimon. Default
    value is zero. See HA section for additional info.

    arp_interval=

    Use integer value for the frequency (in ms) of arp monitoring. Zero value
    is default and means the arp monitoring will be disabled. See HA section
    for additional info. This field is value in active_backup mode only.

    arp_ip_target=

    An ip address to use when arp_interval is > 0. This is the target of the
    arp request sent to determine the health of the link to the target.
    Specify this value in ddd.ddd.ddd.ddd format.

    If you need to configure several bonding devices, the driver must be loaded
    several times. I.e. for two bonding devices, your /etc/conf.modules must look
    like this:

    alias bond0 bonding
    alias bond1 bonding

    options bond0 miimon=100
    options bond1 -o bonding1 miimon=100

    Reply

Anything to add?

X