jsor
jsor

Reputation: 667

XML::Twig add space before the enclosing >

I have :

<data id="010" name="Common"  action="text"> 
...
</data>

how to use XML::Twig to add space before ">" i.e something like :

<data id="010" name="Common"  action="text" > 

</data>

any idea ?

Upvotes: 0

Views: 88

Answers (1)

mirod
mirod

Reputation: 16171

It would not be pretty, but you could change the way XML::Twig outputs start tags. The method is start_tag, in XML::Twig::Elt;

The cleanest way to do this would be to subclass XML::Twig::Elt (as my_elt in the code below) and to change the output of the original start_tag method:

#!/usr/bin/env perl

use strict;
use warnings;

use XML::Twig;
# elements will be created as "my_elt" instead of XML::Twig::Elt                      
my $t= XML::Twig->new( elt_class => 'my_elt')  
                ->parse( '<data id="010" name="Common"> text </data>')
                ->print;


# create a new class, based on 'XML::Twig::Elt'
package my_elt;
use base 'XML::Twig::Elt';

# my_elt only tweaks start_tag
sub start_tag
  { my $s= shift->SUPER::start_tag(); # get the original start tag
    $s=~ s{(>\s*)$}{ $1}s;            # replace the last '>' by ' >'
    return $s;                        # voilà!
  }

Upvotes: 2

Related Questions