horrendo has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I'm trying to use XML::Simple to create, read and update an XML config file but I'm obviously missing something regarding encoding.

When I create the XML file for the first time (using XMLout from XML::Simple), I end up with a file without the usual

<?xml version="1.0" encoding="UTF-8"?>

line at the top of the file. If I then attempt to read in that file using XML::Simple I get the following warning :

Unable to recognise encoding of this document at /usr/lib/perl5/vendor +_perl/5.8.8/XML/SAX/PurePerl/EncodingDetect.pm line 96.

If I manually edit the file created with XMLout and insert the encoding directive line, the warning disappears.

How do I avoid this warning?

Thanks a lot

Replies are listed 'Best First'.
Re: XML::Simple, XMLout and encoding
by liverpole (Monsignor) on Mar 02, 2010 at 00:15 UTC
    Hi horrendo, I haven't done anything with XML before, but my recent work project will require it, so I used your question to "get my feet wet".

    Take a look at the XML::Simple documentation (hint: search for "xml version="), and you'll find:

    XMLDecl => 1 or XMLDecl => 'string' *# out - handy* If you want the output from "XMLout()" to start with the optional +XML declaration, simply set the option to '1'. The default XML declara +tion is: <?xml version='1.0' standalone='yes'?> If you want some other string (for example to declare an encoding value), set the value of this option to the complete string you re +quire.

    That (plus the first couple of lines of the SYNOPSIS) quickly let me arrive at the following code sample, all within a few minutes:

    use strict; use warnings; use XML::Simple; my $h_ref = { 'a' => 1, 'b' => 2, 'c' => 3, 'colors' => [ 'red', 'green', 'blue' ], 'sub_hash' => { 'numbers' => [ 0, 1, 2], 'letters' => { 'vowels' => [qw[a e i o u]], }, }, }; my $ref = "1,2,3"; my $xs = XML::Simple->new(ForceArray => 1, KeepRoot => 1, XMLDecl => +1); my $xml = $xs->XMLout($h_ref); print "XML is:\n$xml\n"; __END__ [Output] <?xml version='1.0' standalone='yes'?> <opt a="1" b="2" c="3"> <colors>red</colors> <colors>green</colors> <colors>blue</colors> <sub_hash> <letters> <vowels>a</vowels> <vowels>e</vowels> <vowels>i</vowels> <vowels>o</vowels> <vowels>u</vowels> </letters> <numbers>0</numbers> <numbers>1</numbers> <numbers>2</numbers> </sub_hash> </opt>

    Update:  Made search string more specific.


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Re: XML::Simple, XMLout and encoding
by almut (Canon) on Mar 02, 2010 at 00:15 UTC

      Thanks very much. That of course is the piece I was missing.

      Regards,

      H
Re: XML::Simple, XMLout and encoding
by Jenda (Abbot) on Mar 02, 2010 at 11:06 UTC

    Apart from the xdecl, you should also consider installing XML::Parser or the compiled version of XML::SAX. The XML::SAX::PurePerl is slow.

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.