Is there a decent way how a XML document (XML::LibXML::Document)
can be serialized ($doc->toString) with diacritics (non ISO-8859-1?)
characters turned into Unicode entities?
If the document comes from parsing XML without heading it occurs naturally.
use XML::LibXML;
my $str = "<flower>r\x{16f}\x{17e}e</flower>";
my $doc = XML::LibXML->new->parse_string($str);
warn "Document ", $doc->toString, "\n";
warn "Encoding ", $doc->encoding, "\n";
yields
Document <?xml version="1.0"?>
<flower>růže</flower>
Encoding
The only way to achieve this effect on already parsed document
which I found is to set the encoding to ISO-8859-1 (since I cannot
"reset" the encoding).
$doc->setEncoding('iso-8859-1')
use XML::LibXML;
my $str = '<?xml version="1.0" encoding="utf8"?>'
. "<flower>r\x{16f}\x{17e}e</flower>";
my $doc = XML::LibXML->new->parse_string($str);
warn "Document ", $doc->toString, "\n";
warn "Encoding ", $doc->encoding, "\n\n";
$doc->setEncoding('iso-8859-1');
warn "Document ", $doc->toString, "\n";
warn "Encoding ", $doc->encoding, "\n";
yields
Document <?xml version="1.0" encoding="utf8"?>
<flower>rĹŻĹže</flower>
Encoding utf8
Document <?xml version="1.0" encoding="iso-8859-1"?>
<flower>růže</flower>
Encoding iso-8859-1
Does this method have any danger or drawback?
Is there a better way how to "clear" the encoding?
I would find it very useful since the serialized text
with entities is imune against any encoding changes
when stored to database (Oracle).
Thanks,
Roman
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.