简体   繁体   中英

Error using xml option to Perl's XML::Tidy

What I would like to ask you is that when I want to tidy the attached file using :

my $tidy_doc = XML::Tidy->new("filename"=>"/Users/.../tidy.xml") ;
$tidy_doc->tidy() ;
$tidy_doc->write() ;

works just fine, but when I want to tidy it using :

my $tidy_doc = XML::Tidy->new("xml"=>$doc) ;

I get the following error :

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

I would appreciate it, if you could help me on this one.

PS. the .xml file is the following :

<?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>

thank you

PS. the $doc is defined as follows :

my $doc = $parser->parse_file("$XMLDEV"."config/".$node_dns."/STATS.xml") ;
if( !$doc ) { warn "---> non well-formed XML file: $@" ; next ; }
my $root = $doc->getDocumentElement ;

Assuming the parse_file function you're using is a part of a module such as XML::LibXML, the problem here is that your are providing XML::Tidy a data structure generated by the module you're using. XML::Tidy expects the plain text of the XML document.

You should do something such as:

#!/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");

An alternative, which makes use of the LibXML module:

# 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");

Note that this should be done as the last step of processing your XML, since it is exporting the XML content via serialization. Further operations on the $doc object will not be reflected in $tidy_doc .

As an aside, please accept answers that solve your questions by clicking the check mark next to them.

https://stackoverflow.com/faq#howtoask

I don't get an error with this code:

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');

I intentionally removed all indentation. The out.xml file I get is properly indented.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM