我有这段代码可以实现ParserCallback
并将HTML
电子邮件转换为Plain
文本。当我像这样解析电子邮件正文时,此代码工作正常=
"DO NOT REPLY TO THIS EMAIL MESSAGE. <br>---------------------------------------<br>\n" +
"nix<br>---------------------------------------<br> Esfghjdfkj\n" +
"</blockquote></div><br><br clear=\"all\"><div><br></div>-- <br><div dir=\"ltr\"><b>Regards <br>Nisj<br>Software Engineer<br></b><div><b>Bingo</b></div></div>\n" +
"</div>"
但是当我解析这种电子邮件正文时,它返回 null,
email = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html charset=us-ascii\"></head><body style=\"word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;\">Got it...so pls send to customer now.<div><br><div style=\"\"><div>On Nov 8, 2013, at 12:31 PM, <a href=\"mailto:xxxxxxx.com\">xxxxxxx.com</a> wrote:</div><br class=\"Apple-interchange-newline\"><blockquote type=\"cite\">Forwarding test.<br>---------------------------------------<br> ABCD.</blockquote></div><br></div></body></html>";
代码 :
import java.io.IOException;
import java.io.StringReader;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.html.HTML.Attribute;
import javax.swing.text.html.HTML.Tag;
import javax.swing.text.html.HTMLEditorKit.Parser;
import javax.swing.text.html.HTMLEditorKit.ParserCallback;
import javax.swing.text.html.parser.ParserDelegator;
public class EmailBody {
public static void main(String[] args) throws IOException
{
String email = "";
class EmailCallback extends ParserCallback
{
private String body_;
private boolean divStarted_;
public String getBody()
{
return body_;
}
@Override
public void handleStartTag(Tag t, MutableAttributeSet a, int pos)
{
if (t.equals(Tag.DIV) && "ltr".equals(a.getAttribute(Attribute.DIR)))
{
divStarted_ = true;
}
}
@Override
public void handleEndTag(Tag t, int pos)
{
if (t.equals(Tag.DIV))
{
divStarted_ = false;
}
}
@Override
public void handleText(char[] data, int pos)
{
if (divStarted_)
{
body_ = new String(data);
}
}
}
EmailCallback callback = new EmailCallback();
Parser parser = new ParserDelegator();
StringReader reader = new StringReader(email);
parser.parse(reader, callback, true);
reader.close();
System.out.println(callback.getBody());
}
}
你能说出原因吗,为什么会这样?