Reputation: 163
I've to HTML unescape a String (HTML escaped XML File (So all val in my String are & lt;TAG& gt;val& lt;/TAG& gt; and so on) Size ~1,4MB so that I can use the unescaped XML in a parser)
The problem I'm running into is that I always get an out of memory exception when I try to get the unescaped String when I'm using StringEscapeUtils.unescapeHtml(String) (apache-commons-lang-2.6 library).
I also tried the method of the basic android api to unescape the string but beside the fact that it was slow as hell the out of memory exception even occured with smaller Strings (~700kb).
Can someone suggest me how I can handle such a String tranformation without running into an out of memory exception?
Upvotes: 2
Views: 646
Reputation: 522
Java has some good core facilities to get do this really simple. The solution below uses regular expression to go through your content and allows you to replace the characters. This solution does require to do a little work in that you need to provide the escape codes. You can find a list of escape codes here [http://www.w3.org/TR/html4/sgml/entities.html][1] or Google the web for others.
Here is the code below:
import java.util.regex.*;
import java.util.*;
public class HtmlUnescape {
public static void main(String[] args){
HashMap<String,String> codes = new HashMap<String,String>();
codes.put("<", "<");
codes.put(">", ">");
codes.put(""", "\"");
String html = "<html><head><title>Hello</title></head><body><h1>The great escape "example"</h1></body></html>";
Matcher matcher = Pattern.compile("&#*\\w\\w\\w?\\w?;").matcher(html);
StringBuffer matchBuffer = new StringBuffer();
while(matcher.find()){
matcher.appendReplacement(matchBuffer, codes.get(matcher.group()));
}
matcher.appendTail(matchBuffer);
System.out.println (matchBuffer.toString());
}
}
What is going on in the code:
Try that. I have no insight on performance of large files such as yours. But, the code is simple enough to where you can tweak it to get the desired performance.
Upvotes: 2