Ah! Encoding problems! Don't you love them?

If the faulty characters look like French characters then chances are that the document you are parsing is in ISO-88591 (all numbers and options should be verified, I am on a slow connection today and can't check them myself). The document should have an XML declaration specifying that encoding (the default encoding is UTF8, maybe UTF16 is accepted too). So the document is not valid XML. You can still tell the parser to use another encoding though. Look for the ProtocolEncoding or something like that for XML::Parser's new method. Adding ProtocolEncoding => 'ISO-8859-1' (there is an example of this in the doc I think) should solve part of your problem.

I say only part because no matter which encoding the original file is in XML::Parser turns it into UTF8, so when you output the data you then get it in UTF8, which you probably don't want.

Actually YOU probably don't care, but unless you are extremely lucky, chances are that the rest of the software you use to display/store/whatever your output does not like UTF8.

What are your otpions there? First you can convert everything back into ISO-8859-1 (which I will call latin 1 from now on because I am tired of typing that stoopid number). You can do that with a substitution that I don't remember right now (darn slow connection!) but that you can find in the code of XML::TiePYX, or using the Unicode::Strings module or using the Perl interface to iconv which name I can't remember either. You can also use the original_string method in the handlers to get the string in the original encoding. The only problem then is that if you have accented characters in attribute values you can't use the values from %atts and you have to hand-parse the opening tag string to extract those values, which is quite error-prone. Perl 5.6.0 also has tr///U and tr///C that supposedly do this but these options are already deprecated and won't be supported in future versions do to changes in the Unicode interface. I'd say your best bet is to get the relevant code from XML::TiePYX, it's a small module, look for 'latin1' in it.

I have written an article about this a while ago for XML.com, you can find a copy here. It has exemples, but does not include the "XML::TiePYX manoeuvre" as I came accross it later.

Good luck, encoding problems are one of the worst problem with XML.


In reply to Re: XML file won't parse properly by mirod
in thread XML file won't parse properly by brpsss

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.