Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何设置XML文件其余部分的格式?_Xml_Perl_Format_Twig - Fatal编程技术网

如何设置XML文件其余部分的格式?

如何设置XML文件其余部分的格式?,xml,perl,format,twig,Xml,Perl,Format,Twig,我有一个XML文件,并使用以下代码对其进行解析: open( $filehandle, "+<$path") or die "cannot open out file out_file:$!"; my $roots = { tag1 => 1 }; my $handlers = { 'tag0/tag1' => $tweak_server, }; my $twig = new XML::Twig(TwigRoots =

我有一个XML文件,并使用以下代码对其进行解析:

open( $filehandle, "+<$path") or die "cannot open out file out_file:$!";
my $roots = { tag1 => 1 };
my $handlers = { 'tag0/tag1' => $tweak_server,
               };
my $twig = new XML::Twig(TwigRoots                => $roots,
                         TwigHandlers             => $handlers,
                         pretty_print             => 'indented',
                         twig_print_outside_roots => \*$filehandle
                        );
$twig->parsefile($path);
$twig->flush($filehandle, pretty_print => 'indented');
close $filehandle;
open($filehandle),+您的XML有效吗(它不是HTML,是吗?!)?一些输入/输出XML示例将非常有用

无论如何,在玩过XML::Twig之后,我可以放心地说它不会去掉空行。不过,您可以通过编写自己的读取函数来解决这个问题,该函数返回XML而不需要额外的换行符

sub readxml {
    my ($fh) = @_;
    local $/;
    my $xml = <$fh>;
    $xml =~ s/\n+/\n/g;
    return $xml;
}

#
$twig->parse(readxml($filehandle));
sub-readxml{
我的($fh)=@;
本地$/;
我的$xml=;
$xml=~s/\n+/\n/g;
返回$xml;
}
#
$twig->parse(readxml($filehandle));
接下来,我不认为twig会漂亮地打印通过“twig\u print\u outside\u roots”的内容。但是,您可以删除“TwigRoots”,或者添加额外的处理程序来获取剩余内容(或者使用“\u default\u”处理程序),这样它将像所有其他节点一样得到漂亮的打印处理