Hi Monks, I'm using XML::DOM to parse, modify then save an XML document. I'm having a problem in that once I save a document which contained character references/entities, they are saved as multibyte characters, without beind re-encoded. XML::DOM then refuses the parse its own output. Please forgive me if I have the terminology wrong, here is an example of what happens:

Input:
<?xml version="1.0"?> <DocumentRoot> <Element Attr="B&#xE4;r" /> </DocumentRoot>
Script:
use XML::DOM; my $file = @ARGV[0]; my $parser = new XML::DOM::Parser(); my $doc; eval { $doc = $parser->parsefile( $file ); }; if ($@) { die "parsefile() failed: $@\n"; } $doc->printToFile($file."_out"); exit;
The output file created has the extended character (a umlaut) written un-encoded. I'm not sure if this will display properly:
<?xml version="1.0"?> <DocumentRoot> <Element Attr="Bär"/> </DocumentRoot>
If I then pass the this output back to the script, I get:
parsefile() failed: not well-formed (invalid token) at line 3, column 17, byte 54 at /usr/ +lib/perl5/XML/Parser.pm line 187
I've had a look in the XML::DOM code, and I reckon that encodeText() would be the place to do it, however this appears to take a list of characters which should be encoded - for Unicode this would be a pretty big list :-) It's quite possible that there's something about hidden nodes which could be relevant - can anyone point me in the right direction?

I guess if all else fails, a bit of dirty regex-ery on the output could work :-P


Thanks!

In reply to XML::DOM not re-encoding character references of unicode characters? by RenalPete

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.