1

I am using jdom 2.0.6 version and received this IllegalDataException:

Error in setText for tokenization: 

it fails on calling the setText() method.

Element text = new Element("Text");
text.setText(doc.getText());

It seems some characters in 'text' it doesn't accept. For two examples:

Originally Posted by Yvette H( 45) Odd socks, yes, no undies yes, no coat yes, no shoes odd. 
ParryOtter said: Posted

Should I specify encoding somewhere or for some other reasons?

user697911
  • 10,043
  • 25
  • 95
  • 169
  • Hi, I have same issue. I don't think it's directly a problem about encoding but rather about illegal characters in XML as said in org.jdom2.Verifier#checkCharacterData I'll Depending about your needs but in my case, I'll try to automatically locate the illegal character and remove it from the text until checkCharacterData(String text) returns null. – Gweltaz Niquel Jul 03 '17 at 09:06

1 Answers1

0

In fact you just have to escape your text which contains illegal characters with CDATA :

Element text = new Element("Text");
text.setContent(new CDATA(doc.getText()));

The reverse operation (reading text escaped with CDATA is transparent in JDOM2, you won't have to escape it back).

For my tests I added an illegal character at the end of my text by creating one from hex value 0x2 like that :

String text = doc.getText();
int hex = 0x2;
text += (char) hex;
Gweltaz Niquel
  • 629
  • 4
  • 14