我想问你的是,当我想使用以下内容整理附件:
my $tidy_doc = XML::Tidy->new("filename"=>"/Users/.../tidy.xml") ;
$tidy_doc->tidy() ;
$tidy_doc->write() ;
效果很好,但是当我想用它来整理它时:
my $tidy_doc = XML::Tidy->new("xml"=>$doc) ;
我收到以下错误:
bash-3.2# ./_demo.pl
Use of uninitialized value $xmld in substitution (s///) at /Library/Perl/5.10.0/XML/Tidy.pm line 59.
Use of uninitialized value $xmld in pattern match (m//) at /Library/Perl/5.10.0/XML/Tidy.pm line 60.
not well-formed (invalid token) at line 1, column 21, byte 21:
XML::LibXML::Document=SCALAR(0x100805140)
====================^
at /System/Library/Perl/Extras/5.10.0/darwin-thread-multi-2level/XML/Parser.pm line 187
如果你能帮助我,我将不胜感激。
PS。 .xml文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<n_skalis>
<perfdata collection="GigabitEthernet0/0">
<info cmd="show interface" detail="GigabitEthernet0/0">
<input_rate>-1</input_rate><output_rate>-1</output_rate></info>
<info cmd="show interface" detail="GigabitEthernet0/0/1">
<output_rate>-1</output_rate></info>
</perfdata>
<perfdata collection="GigabitEthernet1/1">
<info cmd="show interface" detail="GigabitEthernet1/1">
<input_rate>-1</input_rate><output_rate>-1</output_rate></info>
<info cmd="show interface" detail="GigabitEthernet1/1/0">
<output_rate>-1</output_rate></info>
</perfdata>
<perfdata collection="self">
<info cmd="show buffers summary" detail="">
<big_pool>-1</big_pool><small_pool>-1</small_pool></info>
</perfdata>
</n_skalis>
谢谢
PS。 $ doc的定义如下:
my $doc = $parser->parse_file("$XMLDEV"."config/".$node_dns."/STATS.xml") ;
if( !$doc ) { warn "---> non well-formed XML file: $@" ; next ; }
my $root = $doc->getDocumentElement ;
答案 0 :(得分:1)
假设您正在使用的parse_file
函数是XML :: LibXML等模块的一部分,那么这里的问题是您提供的XML :: Tidy是由您模块生成的数据结构使用。 XML :: Tidy期望XML文档的纯文本。
您应该执行以下操作:
#!/usr/bin/perl
use strict;
use warnings;
my $doc;
# use an anonymous code block to limit the scope of the IRS unset
{
# unset IRS (input record seperator)
# this allows us to read whole file at once
local $/=undef;
# open file
open my $fh,"<","./test.xml"
or die "Could not open file: $!";
# read entire file into scalar variable
$doc = <$fh>;
# close file
close $fh;
}
# process file content with XML::Tiday
my $tidy_doc = XML::Tiday->new(xml => $doc);
$tidy_doc->tidy();
$tidy_doc->write("output.xml");
另一种使用LibXML模块的替代方法:
# create XML::LibXML::Document object
my $doc = $parser->parse_file("./test.xml");
# use the the toString function to extract the XML content from the object
my $tidy_doc = XML::Tiday->new(xml => $doc->toString);
$tidy_doc->tidy();
$tidy_doc->write("output.xml");
请注意,这应该是处理XML的最后一步,因为它是通过序列化导出XML内容。 $doc
对象的进一步操作不会反映在$tidy_doc
。
顺便说一下,请点击旁边的复选标记,接受解决问题的答案。
答案 1 :(得分:0)
我的代码没有出错:
use warnings;
use strict;
use XML::Tidy;
my $doc = <<EOF;
<?xml version="1.0" encoding="utf-8"?>
<inode>
<perfdata collection="GigabitEthernet0/0">
<info cmd="show interface" detail="GigabitEthernet0/0">
<input_rate>show_interface_input_rate.tcl</input_rate>
<output_rate>show_interface_output_rate.tcl</output_rate>
</info>
<info cmd="show interface" detail="GigabitEthernet0/0/1">
<output_rate>show_interface_output_rate.tcl</output_rate>
</info>
</perfdata>
<perfdata collection="GigabitEthernet1/1">
<info cmd="show interface" detail="GigabitEthernet1/1">
<input_rate>show_interface_input_rate.tcl</input_rate>
<output_rate>show_interface_output_rate.tcl</output_rate>
</info>
<info cmd="show interface" detail="GigabitEthernet1/1/0">
<output_rate>show_interface_output_rate.tcl</output_rate>
</info>
</perfdata>
<perfdata collection="self">
<info cmd="show buffers summary" detail="">
<big_pool>show_buffers_summary_big_pool.tcl</big_pool>
<small_pool>show_buffers_summary_small_pool.tcl</small_pool>
</info>
</perfdata>
</inode>
EOF
my $tidy_doc = XML::Tidy->new(xml => $doc);
$tidy_doc->tidy();
$tidy_doc->write('out.xml');
我故意删除了所有缩进。我得到的out.xml
文件正确缩进。