我有以下Perl
代码,它读取输入并正确缩进文件。我没有使用xmllint
,或者XML-Tidy
是因为DTD
. 但是,假设我使用下面的代码进行练习:
### 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>
它缩进和打印是正确的......但是,我需要从XML
文件中读取它,缩进然后将其以新格式重新写入同一个文件。我完全是新手Perl
,我怎么能从文件中读取输入,而不是样本数据,然后使用上面的代码缩进?谢谢