Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

drefty

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

XMLPrettyPrint: simple xml pretty print in perl

   1  
   2  ### begin_: file metadata
   3      ### <region-file_info>
   4      ### main:
   5      ###   - name    : XMLPrettyPrint: simple xml pretty print in perl
   6      ###     desc    : use perl with XML::Twig library to print indented xml
   7      ###     date    : created="Thu 2005-12-01 11:08:15"
   8      ###     last    : lastmod="Thu 2005-12-01 11:22:34"
   9      ###     lang    : perl
  10      ###     tags    : perl xml indent formatted pretty string cfPrettyPrint
  11      ### </region-file_info>
  12  
  13  ### begin_: init perl
  14      use strict;
  15      use warnings;
  16      use XML::Twig;
  17  
  18  ### begin_: init vars
  19      my  $sXML  = join "", (<DATA>);
  20  
  21      ### init params
  22      my  $params = [qw(none nsgmls nice indented record record_c)];
  23      my  $sPrettyFormat  = $params->[3] || 'none';
  24  
  25  ### begin_: process
  26      my  $twig= new XML::Twig;
  27      $twig->set_indent(" "x4);
  28      $twig->parse( $sXML );
  29      $twig->set_pretty_print( $sPrettyFormat );
  30      $sXML      = $twig->sprint;
  31  
  32  ### begin_: output
  33      print $sXML;
  34  
  35  ### begin_: sample data
  36      1;
  37      __END__
  38  <table><tr age="35" >
  39  <fname>Homer</fname>
  40  <lname>Simpson</lname></tr>
  41  <tr age="33" >
  42  <fname>Barney</fname>
  43  <lname>Rubble</lname></tr>
  44  <tr age="29" >
  45  <fname>Betty</fname>
  46  <lname>Rubble</lname></tr></table>
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS