leer y sangrar un archivo xml perl

Tengo lo siguiente Perl code, which reads the input and indents the file correctly. I'm not using xmllinto XML-Tidy because of some problems with the DTD. But let's say for exercise I've use the code below:

 ### begin_: file metadata
        ### <region-file_info>
        ### main:
        ###   - name    : XMLPrettyPrint: simple xml pretty print in perl
        ###     desc    : use perl with XML::Twig library to print indented xml
        ###     date    : created="Thu 2005-12-01 11:08:15"
        ###     last    : lastmod="Thu 2005-12-01 11:22:34"
        ###     lang    : perl
        ###     tags    : perl xml indent formatted pretty string cfPrettyPrint
        ### </region-file_info>

### begin_: init perl
    use strict;
    use warnings;
    use XML::Twig;

### begin_: init vars
    my  $sXML  = join "", (<DATA>);

    ### init params
    my  $params = [qw(none nsgmls nice indented record record_c)];
    my  $sPrettyFormat  = $params->[3] || 'none';

### begin_: process
    my  $twig= new XML::Twig;
    $twig->set_indent(" "x2);
    $twig->parse( $sXML );
    $twig->set_pretty_print( $sPrettyFormat );
    $sXML      = $twig->sprint;

### begin_: output
    print $sXML;

### begin_: sample data
    1;
    __END__
<table><tr age="35" >
<fname>Homer</fname>
<lname>Simpson</lname></tr>
<tr age="33" >
<fname>Barney</fname>
<lname>Rubble</lname></tr>
<tr age="29" >
<fname>Betty</fname>
<lname>Rubble</lname></tr></table>

It indents and print is correctly ... however, I needed to read it from an XML file, indent and then right it back to the same file, with the new format. I'm totally new to Perl, how could I read the input from a file, instead of a sample data, and then use that above code to indent? Thanks

preguntado el 28 de agosto de 12 a las 14:08

4 Respuestas

¿Qué tal

my $file= $ARGV[0];
XML::Twig->new( pretty_print => 'indented')
         ->parsefile( $file )
         ->print_to_file( $file);

No need to set the indent to 2 spaces, that's the default. The 1 at the end is also not needed, it is only needed for modules, to inform the parser that they have loaded properly.

Esto también es equivalente a xml_pp -i myfile.xml

Respondido 28 ago 12, 15:08

The easiest way to read and write whole files is probably the module File::Slurp. It makes it as easy as this:

use File::Slurp;
my $content = read_file('some_file.xml');
# do stuff
write_file('some_file.xml', $content);

Respondido 28 ago 12, 14:08

XML :: Simple is what you need. It doesn't care about DTDs. It can read and write your XML and indent it as well.

Respondido 28 ago 12, 14:08

For who comes here from their future, the docs say now "You really don't want to use this module in new code. If you ignore this warning and use it anyway, the qw(:strict) mode will save you a little pain." - Niccolò

Ver habiertos para detalles:

open my $XML, '<', $filename or die $!;
my $sXML  = join "", (<$XML>);
close $XML;

# ...

open my $OUT, '>', 'file.xml' or die $!;
print {$OUT} $sXML;
close $OUT;

Respondido 28 ago 12, 14:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.