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 :

    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.
    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->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 :


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.


Related Post

3 Responses

  1. robfish88 says:

    Thank you very much…….

  2. robfish88 says:

    Kindly advice, I want to use OpenWRT for my TP-Link router model TL-WR1043ND version 1.10 starting serial number with 12.

    Requested for UNIFI Malaysia 20 MB

    Kindly advice which files I do need to download, highly appropriated in advance .



    (Shah Alam)

  3. robfish88 says:

    Good Morning ,
    I have a TL-WR1043ND router Ver 1.10 and not able to find Hardware version, as what I understood from other posts that this should be then Hardware version 1.
    Have installed OpenWRT in total 3 files :
    German stock firmware (v3.9.17)
    – attitude_adjustment 12.09-rc2 and
    attitude_adjustment 12.09-rc2 and able to able access my router TL-WRT1043ND

    I guess that it is my age (59) but I a losing it on the configuration and explanation, first the first of all on many images that I have seen on the internet in the part Internet -> Swith VLAN witch is starting with PORT 1 -2-3-4-5-CPU however on my installed OpenWRT it starts with PORT 0-1-2-3-4-CPU so I guess something is already wrong on my side, having said this most images are related to HARDWARE VERSION 1.8

    My main goal is to get connect to UNIFI Malaysia (20MB connection) I know that Unifi provides an IPTV package but to honest I don’t need this IPTV connection at all.

    The bottom line, I like get connected to UNIFI with OPENWRT and I am really begging f someone can assist me in to get connect to Unifi. I know it is labor day (May 1) but will be great full if somebody can assist me.


Anything to add?