You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by Stéphane Kugge <st...@codeonline.com> on 2000/05/27 04:43:52 UTC

Problems with encoding and whitespaces

Hello,

Our company has just moved from xml4j to Xerces and it was not 
that simple at all. Firstable I had the bad luck to use IBM TX-
implementation classes that provided some handy methods. This 
time I only used org.w3c.dom interfaces. Anyway I encountered 
several problems (3 so far), and one was solved (not prettily 
though):

1) Starting with the solved problem: one was able to read the 
encoding of an XML file with a method in IBM TXDocument. Why 
Xerces DocumentImpl does not have such a method. Why 
org.w3c.dom.Document interface even does not have a method to 
access the XML header ? Anyway I can read the encoding now by 
overriding the xmldecl method in DOMParser (not pretty though).

2) Why does Xerces systematically escape non UTF-8 characters 
when serializing a DOM tree (and I DO set the encoding in 
OutputFormat class). I have an XML document encoded in SJIS 
with xml4j and its size tripled after I saved it for the first time with
Xerces. Even simple ISO-8859-1 characters for french éèàù... are all
escaped. If this is default behaviour, how can I save to the real 
encoding
(SJIS, ISO...) like in xml4j (well we needed to pass the encoding to 
the
OutputStream constructor for the stuff to work). Escaped 
characters take
up space (2-8 x more) require additional and tedious parsing.

3) Now comes the issue about significant whitespaces in attributes 
and text nodes. I have carriage returns in attributes (I admit it 
sucks, I was asking my colleagues to make them text nodes, but 
so much as been done already that my life could be jeopardized) 
that disappear (replaced by spaces) after parsing. I think that is an 
XML
recommended behaviour ? If it is then why Xerces serializes my 
attributes
with CRLF as such? why save something that I won't get back when
opening/parsing the document? Well here, I wouldn't mind that the
serializer escapes my CRLF to &#10; My question is now how can 
I define
CRLF in attributes? what is a good approach: - escape them as 
&#10; or
anything else: \n ... - leave them as CRLF and setting the parser 
not to
replace them with spaces (if it involves overriding a method in 
DOMParser
class, which one then ?). - well something else that does not involve
dramatic changes in our DTD (my life is at stake).

I am currently working on an improved version of our XML-based 
document format. We are using text nodes more intensively for text 
meant to be displayed: I have currently my own 
removewhitespaces() method that trims front and end of the text 
node strings and deletes the empty text nodes from the tree. Is-
there a Xerces way to do that (I mean without validating the 
document, and it should not be more than three lines to type ... OK 
four, but that's my last offer).

I hope you have time to read the story of my life.
Thank you for your time, dedication and good work.

Regards