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&#x16F;&#x17E;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&#313;&#379;&#313;že</flower> Encoding utf8 Document <?xml version="1.0" encoding="iso-8859-1"?> <flower>r&#367;&#382;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

In reply to XML::LibXML document serialized with diacritics as unicode entities by roman

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.