开发者

Android Out of Memory Exception while HTML unescaping a String

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 开发者_C百科out of memory exception?


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("&lt;", "<");
        codes.put("&gt;", ">");
        codes.put("&#34;", "\"");

        String html = "&lt;html&gt;&lt;head&gt;&lt;title&gt;Hello&lt;/title&gt;&lt;/head&gt;&lt;body&gt;&lt;h1&gt;The great escape &#34;example&#34;&lt;/h1&gt;&lt;/body&gt;&lt;/html&gt;";

        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:

  • First, the hash stores the codes to unescape.
  • Second, variable html stores escape HTML to process.
  • Next, we use the regex expression to search and replace the escaped codes using:
    • Matcher.find(),
    • Matcher.appendReplacement(), and
    • Matcher.appendTail() methods.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜