may be
//remove the trailing white space
String medianRes = xmlString.replaceAll("\\s+$","");
//remove other white space
String res = medianRes.replaceAll("\\s+$"," ");
^ is meaning start with some char; $ is meaning end with some char;