亚洲欧美日韩中文不卡|eeuss电影天堂一区二区|国产美女一级做a爱视频|精品五夜婷香蕉国产线看观看|国产综合另类小说色区色噜噜

小程序rich-text組件,解析html——廈門(mén)小程序開(kāi)發(fā),泉州小程序開(kāi)發(fā),漳州小程序開(kāi)發(fā)

行業(yè)動(dòng)態(tài) 公司新聞 案例分享 技術(shù)百科

小程序rich-text組件——html解析

來(lái)源:奇站網(wǎng)絡(luò ) 瀏覽量:3,234 發(fā)布日期: 2018-03-08

基于java開(kāi)發(fā)小程序接口,后臺使用ueditor編輯圖文內容。
小程序如何顯示富文本內容呢?

一、直接傳入html

nodes 屬性推薦使用 Array 類(lèi)型,由于組件會(huì )將 String 類(lèi)型轉換為 Array 類(lèi)型,因而性能會(huì )有所下降

直接將html內容傳入是可以,但是無(wú)論從性能還是顯示樣式來(lái)說(shuō)都會(huì )有問(wèn)題,所以不推薦這樣做。

二、wxParse

網(wǎng)上現在較多的解決方法都是在小程序里引入wxParse插件來(lái)解決。
考慮到引入額外的插件會(huì )使得程序變大,所以就沒(méi)有詳細研究,有興趣的可以自行百度。

三、解析成json

  1. public class RichTextParse {
  2. public static List<Object> parse(String body) throws DocumentException {
  3. List<Object> nodes = new ArrayList<Object>();
  4. Document doc = null;
  5. doc = DocumentHelper.parseText("<xml>" + body + "</xml>"); // 將字符串轉為XML
  6. Element rootElt = doc.getRootElement(); // 獲取根節點(diǎn)
  7. List<Element> list = rootElt.elements();// 獲取根節點(diǎn)下所有節點(diǎn)
  8. for (Element element : list) { // 遍歷節點(diǎn)
  9. RichTextNode node = new RichTextNode();
  10. node.setName(element.getName());
  11. // attrs
  12. for (Iterator it = element.attributeIterator(); it.hasNext();) {
  13. Attribute attr = (Attribute) it.next();
  14. node.getAttrs().put(attr.getName(), attr.getText());
  15. }
  16. // has children
  17. if (!element.isTextOnly()) {
  18. loopElement(node, element);
  19. } else {
  20. RichTextNodeText nodeText = new RichTextNodeText();
  21. nodeText.setType("text");
  22. nodeText.setText(element.getText());
  23. node.getChildren().add(nodeText);
  24. }
  25. // add to nodes
  26. nodes.add(node);
  27. }
  28. return nodes;
  29. }
  30. private static void loopElement(RichTextNode nodeParent, Element elementParent) {
  31. List<Element> eles = elementParent.elements();
  32. for (Element element : eles) {
  33. RichTextNode node = new RichTextNode();
  34. node.setName(element.getName());
  35. // attrs
  36. for (Iterator it = element.attributeIterator(); it.hasNext();) {
  37. Attribute attr = (Attribute) it.next();
  38. node.getAttrs().put(attr.getName(), attr.getText());
  39. }
  40. //
  41. switch (element.getName()) {
  42. case "img":
  43. node.getAttrs().put("style", "max-width:100%;height:auto;");
  44. break;
  45. default:
  46. break;
  47. }
  48. // has children
  49. if (!element.isTextOnly()) {
  50. loopElement(node, element);
  51. } else {
  52. RichTextNodeText nodeText = new RichTextNodeText();
  53. nodeText.setType("text");
  54. nodeText.setText(element.getText());
  55. node.getChildren().add(nodeText);
  56. }
  57. // add to parent node
  58. nodeParent.getChildren().add(node);
  59. }
  60. }
  61. }
  1. public class RichTextNode {
  2. private String name;
  3. private HashMap<String, String> attrs;
  4. private List<Object> children;
  5. public RichTextNode() {
  6. super();
  7. this.attrs = new HashMap<String, String>();
  8. this.children = new ArrayList<Object>();
  9. }
  10. public String getName() {
  11. return name;
  12. }
  13. public void setName(String name) {
  14. this.name = name;
  15. }
  16. public HashMap<String, String> getAttrs() {
  17. return attrs;
  18. }
  19. public void setAttrs(HashMap<String, String> attrs) {
  20. this.attrs = attrs;
  21. }
  22. public List<Object> getChildren() {
  23. return children;
  24. }
  25. public void setChildren(List<Object> children) {
  26. this.children = children;
  27. }
  28. }
  1. public class RichTextNodeText {
  2. private String type;
  3. private String text;
  4. public String getType() {
  5. return type;
  6. }
  7. public void setType(String type) {
  8. this.type = type;
  9. }
  10. public String getText() {
  11. return text;
  12. }
  13. public void setText(String text) {
  14. this.text = text;
  15. }
  16. }

這里測試了簡(jiǎn)單的圖文編輯沒(méi)有問(wèn)題(html層級只有2層),暫未測試更復雜的多層嵌套的html(例如直接復制網(wǎng)頁(yè)內容粘貼過(guò)來(lái))。

后續發(fā)現將html當成簡(jiǎn)單xml來(lái)解析只能處理簡(jiǎn)單的內容,最后改成jsoup來(lái)解析
http://www.cn-dianji.com/article_90.html

標簽:

廈門(mén)奇站網(wǎng)絡(luò )科技有限公司

電話(huà):13313868605

QQ:3413772931

地址:廈門(mén)集美區軟件園三期

網(wǎng)站地圖


                    掃一掃加我咨詢(xún)

亚洲欧美日韩中文不卡|eeuss电影天堂一区二区|国产美女一级做a爱视频|精品五夜婷香蕉国产线看观看|国产综合另类小说色区色噜噜