0

我想问你的是,当我想整理附件时使用:

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 ;
4

2 回答 2

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.

顺便说一句,请通过单击旁边的复选标记来接受可以解决您问题的答案。

https://stackoverflow.com/faq#howtoask

于 2011-04-22T18:31:00.677 回答
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文件是正确缩进的。

于 2011-04-22T17:07:12.390 回答