<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup --><dependency><groupId>org.jsoup</groupId><artifactId>jsoup</artifactId><version>1.11.2</version></dependency>
public class RichTextParse {public static List<Object> parse(String body) throws Exception {List<Object> nodes = new ArrayList<Object>();Document doc = Jsoup.parse(body);Element rootElement = doc.body();for (Element element : rootElement.children()) {RichTextNode node = new RichTextNode();node.setName(element.nodeName());// attrsfor (Attribute attr : element.attributes()) {node.getAttrs().put(attr.getKey(), attr.getValue());}// has childrenif (element.children().size() > 0) {loopElement(node, element);} else {RichTextNodeText nodeText = new RichTextNodeText();nodeText.setType("text");nodeText.setText(element.text());node.getChildren().add(nodeText);}// add to nodesnodes.add(node);}return nodes;}private static void loopElement(RichTextNode nodeParent, Element elementParent) {List<Element> eles = elementParent.children();for (Element element : eles) {RichTextNode node = new RichTextNode();node.setName(element.nodeName());// attrsfor (Attribute attr : element.attributes()) {node.getAttrs().put(attr.getKey(), attr.getValue());}//switch (element.nodeName()) {case "img":node.getAttrs().put("style", "max-width:100%;height:auto;");break;default:break;}// has childrenif (element.children().size() > 0) {loopElement(node, element);} else {RichTextNodeText nodeText = new RichTextNodeText();nodeText.setType("text");nodeText.setText(element.text());node.getChildren().add(nodeText);}// add to parent nodenodeParent.getChildren().add(node);}}}
標簽:
- 版權所有:奇站網(wǎng)絡(luò ) 轉載請注明出處
- 廈門(mén)奇站網(wǎng)絡(luò )科技有限公司,專(zhuān)業(yè)提供網(wǎng)站建設,響應式網(wǎng)站建設,小程序開(kāi)發(fā),系統定制開(kāi)發(fā)。
- 軟件開(kāi)發(fā)咨詢(xún)熱線(xiàn):吳小姐 13313868605
