[jdom-interest] String length shorten after .getChild().getText() is being used.

Tatu Saloranta cowtowncoder at yahoo.com
Tue Apr 1 09:52:11 PDT 2008


--- Jacques wong <jacques_wong at hotmail.com> wrote:

> Hi,
>  I'm using JDOM v1.1. Basically, I can use most of
> the function of the JDOM, but I found some stranges
> when I use Element.getChild().getText(); I've an XML
> that contain some big5 characters (externally
> created XML file), both using XMLOutputter for
> outputting screen and XML file have no affection on
> the big5 codeset displays. However, when I tried to
> query each text one by one by using
> Element.getChild().getText(), the String returned
> always is shorter than the original in XMLfile, and
> the Big5 characters are displayed incorrectly. I
> tried to use the conversion. String s = new

Shorter as measured by... ? Number of characters in
it? Since JDOM is not a parser, encoding/decoding
issues are dealt with by the underlying parser;
default being Xerces when using JDK 1.5+.

I doubt JDOM has anything to do with the problem. By
the time it gets data from parser, it's all in java
chars/Strings, decoded from input (byte stream
usually) as necessary.
But without a sample document it is impossible to know
what exactly goes wrong.

The most common error is that the encoding declaration
in the xml document is wrong, and contents are encoded
using some other encoding.
Second common problem is developers printing out text
to console, and console being unable to display it
properly.

>
String(recElement.getChild("ThxRegTxt").getText().getBytes("UTF-8"),"big5");
> but seems it's not displaying correctly also. My

No kidding, that's about worst piece of code anyone
could write. I wish compilers would refuse to compile
it. :-p
If it worked as expected, your input data was broken,
and you were just lucky that 2 wrongs made right.

-+ Tatu +-



      ____________________________________________________________________________________
You rock. That's why Blockbuster's offering you one month of Blockbuster Total Access, No Cost.  
http://tc.deals.yahoo.com/tc/blockbuster/text5.com


More information about the jdom-interest mailing list