基于java開發(fā)小程序接口,后臺使用ueditor編輯圖文內(nèi)容。
小程序如何顯示富文本內(nèi)容呢?
一、直接傳入html
nodes 屬性推薦使用 Array 類型,由于組件會將 String 類型轉(zhuǎn)換為 Array 類型,因而性能會有所下降
直接將html內(nèi)容傳入是可以,但是無論從性能還是顯示樣式來說都會有問題,所以不推薦這樣做。
二、wxParse
網(wǎng)上現(xiàn)在較多的解決方法都是在小程序里引入wxParse插件來解決。
考慮到引入額外的插件會使得程序變大,所以就沒有詳細(xì)研究,有興趣的可以自行百度。
三、解析成json
public class RichTextParse {
public static List<Object> parse(String body) throws DocumentException {
List<Object> nodes = new ArrayList<Object>();
Document doc = null;
doc = DocumentHelper.parseText("<xml>" + body + "</xml>"); // 將字符串轉(zhuǎn)為XML
Element rootElt = doc.getRootElement(); // 獲取根節(jié)點
List<Element> list = rootElt.elements();// 獲取根節(jié)點下所有節(jié)點
for (Element element : list) { // 遍歷節(jié)點
RichTextNode node = new RichTextNode();
node.setName(element.getName());
// attrs
for (Iterator it = element.attributeIterator(); it.hasNext();) {
Attribute attr = (Attribute) it.next();
node.getAttrs().put(attr.getName(), attr.getText());
}
// has children
if (!element.isTextOnly()) {
loopElement(node, element);
} else {
RichTextNodeText nodeText = new RichTextNodeText();
nodeText.setType("text");
nodeText.setText(element.getText());
node.getChildren().add(nodeText);
}
// add to nodes
nodes.add(node);
}
return nodes;
}
private static void loopElement(RichTextNode nodeParent, Element elementParent) {
List<Element> eles = elementParent.elements();
for (Element element : eles) {
RichTextNode node = new RichTextNode();
node.setName(element.getName());
// attrs
for (Iterator it = element.attributeIterator(); it.hasNext();) {
Attribute attr = (Attribute) it.next();
node.getAttrs().put(attr.getName(), attr.getText());
}
//
switch (element.getName()) {
case "img":
node.getAttrs().put("style", "max-width:100%;height:auto;");
break;
default:
break;
}
// has children
if (!element.isTextOnly()) {
loopElement(node, element);
} else {
RichTextNodeText nodeText = new RichTextNodeText();
nodeText.setType("text");
nodeText.setText(element.getText());
node.getChildren().add(nodeText);
}
// add to parent node
nodeParent.getChildren().add(node);
}
}
}
public class RichTextNode {
private String name;
private HashMap<String, String> attrs;
private List<Object> children;
public RichTextNode() {
super();
this.attrs = new HashMap<String, String>();
this.children = new ArrayList<Object>();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public HashMap<String, String> getAttrs() {
return attrs;
}
public void setAttrs(HashMap<String, String> attrs) {
this.attrs = attrs;
}
public List<Object> getChildren() {
return children;
}
public void setChildren(List<Object> children) {
this.children = children;
}
}
public class RichTextNodeText {
private String type;
private String text;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
這里測試了簡單的圖文編輯沒有問題(html層級只有2層),暫未測試更復(fù)雜的多層嵌套的html(例如直接復(fù)制網(wǎng)頁內(nèi)容粘貼過來)。
后續(xù)發(fā)現(xiàn)將html當(dāng)成簡單xml來解析只能處理簡單的內(nèi)容,最后改成jsoup來解析
http://m.yywcj.com/article_90.html
- 版權(quán)所有:奇站網(wǎng)絡(luò) 轉(zhuǎn)載請注明出處
- 廈門奇站網(wǎng)絡(luò)科技有限公司,專業(yè)提供網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站建設(shè),小程序開發(fā),系統(tǒng)定制開發(fā)。
- 軟件開發(fā)咨詢熱線:吳小姐 13313868605