Tuesday, April 27, 2010

Getting rid of empty XML elements using XML::Simple

Sometimes it is necessary to remove empty XML elements from XML documents (need a condensed message, etc) and doing so can be a difficult task to achieve! Well, here comes XML::Simple to the rescue...
#!/usr/bin/perl -w
use strict;
use XML::Simple;
use Data::Dumper;
my $xml = <<EOF;
<fullletter>
<CASE_EPIS>00036</CASE_EPIS>
   <letter>
      <IMAGENAME>02_20100423124541</IMAGENAME>
      <RUNDATE>2010-04-23 12:04:41</RUNDATE>
       <LETTERTYPE>LOI2</LETTERTYPE>
   </letter>
    <service> </service>
       <service>
           <NTV_SEQUENCE>26</NTV_SEQUENCE>
           <NTV_DISPLAY_VALUE>Need start date </NTV_DISPLAY_VALUE>
      </service>
       <service> </service>
</fullletter>
EOF

# use XML simple to process the XML
my $xs = XML::Simple->new(

      # remove extra whitespace
      NormaliseSpace => 2,
      # keep root element
      KeepRoot       => 1,
      # force elements to arrays
      ForceArray     => 1,
      # ignore empty elements
      SuppressEmpty  => 1
);
# read in the XML
my $ref = $xs->XMLin($xml);

# print out the data structure
# shows you how to access the data
print Dumper($ref);

# print out the XML minus the empty tags
print $xs->XMLout($ref);
Running this sample code then produces (minus the Data::Dumper text):
<fullletter>
  <CASE_EPIS>00036</CASE_EPIS>
  <letter>
    <IMAGENAME>02_20100423124541</IMAGENAME>
    <LETTERTYPE>LOI2</LETTERTYPE>
    <RUNDATE>2010-04-23 12:04:41</RUNDATE>
  </letter>
  <service>
    <NTV_DISPLAY_VALUE>Need start date</NTV_DISPLAY_VALUE>
    <NTV_SEQUENCE>26</NTV_SEQUENCE>
  </service>
</fullletter>