처음으로 웹메일을 작업하게 됐다.
현재 웹메일 전송 구조가 어떻게 되냐면..
serverlet 단에서 html을 작업후 메일전송 DB에 insert하여 큐에 저장하는 방식이다.
HTML 작성하는 로직이 JAVA로 작업되어 있어서 Element 컨트롤이 너무 어렵고 코드도 복잡하다.
아무튼 AS-IS 코드를 보니까 유일하게 하나 건질만한것이 있었다. JSoup이라는 라이브러리를 알게된것!
jsoup Java HTML Parser, with the best of HTML5 DOM methods and CSS selectors.
jsoup: Java HTML Parser jsoup is a Java library for working with real-world HTML. It provides a very convenient API for fetching URLs and extracting and manipulating data, using the best of HTML5 DOM methods and CSS selectors. jsoup implements the WHATWG H
jsoup.org
웹크롤링 하는데 많이 쓰인다고 한다. 실제로 get을해서 외부로부터 HTML 문서를 가져올 수 있다!
사용방법은 크게 어렵지 않다.
String html = "<html><head><title>First parse</title></head>"
+ "<body><p>Parsed HTML into a doc.</p></body></html>";
Document doc = Jsoup.parse(html);
하지만 문제점이 있다.
String html = "<tr><td> 아우 졸려 </td></tr>";
Element tr = Jsoup.parse(html);
//tr.toString();의 결과는?
이때 tr.toString()의 결과가 <tr><td> 아우 졸려 </td></tr>가 아니라, 완전한 html 문서형태로 자동으로 바꿔준다는것!
+ 정리하자면 HTML 문법을 한번 검증해서 결과를 반환해준다.
쓸데없이 친절하잖아!
공식 문서에도 이렇게 나와있다.
The parser will make every attempt to create a clean parse from the HTML you provide, regardless of whether the HTML is well-formed or not. It handles:
- reliably creating the document structure (html containing a head and body, and only appropriate elements within the head)
그래서 꼼수로...
String html = "<tr><td> 아우 졸려 </td></tr>";
Element orderItemElement = new Element(Tag.valueOf("tr"), "");
orderItemElement.html(html);
tr element를 생성하고, tr Element안에 <tr>을 넣어버렸다.
그랬더니 정상적으로 <tr>을 유지한다.
오늘의 뻘짓 끝..
'Today I learned' 카테고리의 다른 글
2021 02 03 - 검색시스템 (0) | 2021.02.03 |
---|---|
2020 01 06 - Appium (0) | 2021.01.06 |
2020 01 05 - Appium With Docker! (0) | 2021.01.05 |
2021 01 04 - Docker! (0) | 2021.01.04 |
2021 계획 (0) | 2021.01.01 |
댓글