Reputation: 13165
I am getting the value from webservice while unescape xml value. I am storing it in a stringBuffer. I am getting a lot of values from webservice. So I am getting out of memory error. Can anybody tell me how to avoid that?
My UnescapeXml code:
public String unescapeXML(String str) {
if (str == null || str.length() == 0)
return "";
StringBuffer buf = new StringBuffer();
int len = str.length();
for (int i = 0; i < len; ++i) {
char c = str.charAt(i);
if (c == '&') {
int pos = str.indexOf(";", i);
if (pos == -1) { // Really evil
buf.append('&');
} else if (str.charAt(i + 1) == '#') {
int val = Integer.parseInt(str.substring(i + 2, pos), 16);
buf.append((char) val);
i = pos;
} else {
String substr = str.substring(i, pos + 1);
if (substr.equals("&"))
buf.append('&');
else if (substr.equals("<"))
buf.append('<');
else if (substr.equals(">"))
buf.append('>');
else if (substr.equals("""))
buf.append('"');
else if (substr.equals("'"))
buf.append('\'');
else if (substr.equals(" "))
buf.append(" ");
else
// ????
buf.append(substr);
i = pos;
}
} else {
buf.append(c);
}
}
return buf.toString();
}
Thanks
Upvotes: 0
Views: 940
Reputation: 324
Not sure if this is what you are doing but it actually looks like you are simply replacing the html entities with the according character. There are already functions provided in the Document
class. You might be looking for normalizeDocument()
. You should really use streams instead of strings in this case.
Upvotes: 0
Reputation: 3924
You should switch to streaming the data and unescaping it as part of a custom Stream or Reader class as opposed to trying to read everything at once.
Upvotes: 1