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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |