基于java開(kāi)發小(xiǎo)程序接口,後台使用ueditor編輯圖文内容。
小(xiǎo)程序如何顯示富文本内容呢?
一(yī)、直接傳入html
nodes 屬性推薦使用 Array 類型,由于組件會将 String 類型轉換爲 Array 類型,因而性能會有所下(xià)降
直接将html内容傳入是可以,但是無論從性能還是顯示樣式來說都會有問題,所以不推薦這樣做。
二、wxParse
網上現在較多的解決方法都是在小(xiǎo)程序裏引入wxParse插件來解決。
考慮到引入額外(wài)的插件會使得程序變大(dà),所以就沒有詳細研究,有興趣的可以自行百度。
三、解析成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>"); // 将字符串轉爲XML
Element rootElt = doc.getRootElement(); // 獲取根節點
List<Element> list = rootElt.elements();// 獲取根節點下(xià)所有節點
for (Element element : list) { // 遍曆節點
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層),暫未測試更複雜(zá)的多層嵌套的html(例如直接複制網頁内容粘貼過來)。
後續發現将html當成簡單xml來解析隻能處理簡單的内容,最後改成jsoup來解析
901.html
- 版權所有:奇站網絡 轉載請注明出處
- 廈門市中資源網絡服務有限公司,專業提供網站建設,響應式網站建設,小(xiǎo)程序開(kāi)發,系統定制開(kāi)發。
- 軟件開(kāi)發咨詢熱線:吳小(xiǎo)姐 13313868605