سؤال

I am trying to instruct Xstream to convert an XML into an Object but only read it in UTF-8. This is how i am trying to achieve this:

FileInputStream inStream = new FileInputStream((String) original);
InputStreamReader reader = new InputStreamReader(inStream, Charset.forName("UTF-8"));

xstream.fromXML(reader, myObject);

The above does the marshalling but for some reason the following text :

Lorem Ipsum &#xA7

Is always converted as :

Lorem Ipsum §

I dont want it to do any conversion so i want the text to remain as Lorem Ipsum &#xA7. Am i doing something wrong here?

هل كانت مفيدة؟

المحلول

Unfortunately, almost all XML parsers automatically resolve character references, thus the character reference in your string (&#xA7) is being resolved to the real underlying Unicode character.

I've looked at the underlying implementation of XPP3, the Oracle Java DOM Parser, Stax Parser, and the SAX Parser. All of them automatically resolve character references, with no option to disable that (you can get the SAX Parser to not resolve character references in attributes, but not in the text of an element, and only through some nasty trickery)

So, other than writing your own XML Parser, you've probably only got two choices:

1) let the parsers / XStream resolve the character, and then 're-encode' any special characters in your object

2) pre-process the XML stream before it's parsed and either escape the & character (ie. if you replaced it with § then it would work properly).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top