<!-- 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());
// attrs
for (Attribute attr : element.attributes()) {
node.getAttrs().put(attr.getKey(), attr.getValue());
}
// has children
if (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 nodes
nodes.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());
// attrs
for (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 children
if (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 node
nodeParent.getChildren().add(node);
}
}
}