- 查看API实现的方式
- 了解其返回值的解析方式
- 使用Java类进行封装并且实现返回xml的String到前台
查看API的实现方式,dhtmlx的官方文档中
<?xml version='1.0' encoding='utf-8'?> <rows parent='000125103260000300084800'> <row id='000103052220000300207475' xmlkids='1'> <cell image='305.gif'>C</cell> <cell> <![CDATA[01]]> </cell> <userdata name='canRefresh'>true</userdata> </row> </rows>
<?xml version='1.0' encoding='utf-8'?> <rows parent="000125103260000300084800"> <row> <userdata name='canRefresh'>true</userdata> <cell image='o.gif'> <![CDATA[A]]> </cell> <cell image='o.gif'> <![CDATA[01]]> </cell> </row> </rows>
从xml文档中便可得知:
1.Rows 节点当中存放的是Row的节点 一对多的关系,同时属性parent表示的是Row节点id的属性表示的上下级的关系
2.Row节点当中有Userdata的属性 是一对多的关系,可以放入多个值来表示在列当中不能显示的一些值,例如当前行的ID
3.Row节点当中含有Cell节点,是一对多的关系 Cell表示的是每行当中的单元格
根据上面的分析,开始封装代码
Cell.java
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 /** 2 * @Title Cell.java 3 * @Package 4 * @author 5 * @date 2015-10-12 上午11:02:43 6 * @version V1.0 7 */ 8 9 10 /** 11 * @ClassName Cell 12 * @author 13 * @date 2015-10-12 上午11:02:43 14 */ 15 public class Cell { 16 private String image = ""; 17 private String value = ""; 18 19 public Cell() { 20 } 21 22 public Cell(String value) 23 { 24 this.value = value; 25 } 26 public Cell(String value, String image) { 27 this.image = image; 28 this.value = value; 29 } 30 31 /** 32 * @return the image 33 */ 34 public String getImage() { 35 return image; 36 } 37 38 /** 39 * 设置图片路径 40 * 41 * @param image 42 * the image to set 43 */ 44 public void setImage(String image) { 45 this.image = image; 46 } 47 48 /** 49 * 获取cell当中的值 50 * 51 * @return the value 52 */ 53 public String getValue() { 54 return value; 55 } 56 57 /** 58 * 设置cell当中的值 59 * 60 * @param value 61 * the value to set 62 */ 63 public void setValue(String value) { 64 this.value = value; 65 } 66 67 /* (non-Javadoc) 68 * @see java.lang.Object#toString() 69 */ 70 @Override 71 public String toString() { 72 return "Cell [image=" + image + ", value=" + value + "]"; 73 } 74 75 }
在Cell对象中,
private String image = "";
private String value = "";
image 表示的是image的名称,Value则是Cell<cell>value</cell>的值
UserData.java
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 /** 2 * @Title UserData.java 3 * @Package 4 * @author 5 * @date 2015-10-12 上午10:58:35 6 * @version V1.0 7 */ 8 package 9 10 /** 11 * @ClassName UserData 12 * @author 13 * @date 2015-10-12 上午10:58:35 14 */ 15 public class UserData { 16 private String Name = ""; 17 private String value = ""; 18 19 /** 20 * @param name 21 * @param value 22 */ 23 public UserData(String name, String value) { 24 super(); 25 Name = name; 26 this.value = value; 27 } 28 29 /** 30 * 31 */ 32 public UserData() { 33 super(); 34 this.Name = ""; 35 this.value=""; 36 } 37 38 /** 39 * 获取属性name的值 40 * @return the name 41 */ 42 public String getName() { 43 return Name; 44 } 45 /** 46 * 设置属性name的值 47 * @param name the name to set 48 */ 49 public void setName(String name) { 50 Name = name; 51 } 52 /** 53 * 获取value的值 true 54 * <userdata name='canRefresh'>true</userdata> 55 * @return the value 56 */ 57 public String getValue() { 58 return value; 59 } 60 /** 61 * 设置value的值 true 62 * <userdata name='canRefresh'>true</userdata> 63 * @param value the value to set 64 */ 65 public void setValue(String value) { 66 this.value = value; 67 } 68 69 /* (non-Javadoc) 70 * @see java.lang.Object#toString() 71 */ 72 @Override 73 public String toString() { 74 return "UserData [Name=" + Name + ", value=" + value + "]"; 75 } 76 77 78 }
在UserData对象中,
private String Name = "";
private String value = "";
name表示的是属性的name的值,value则是<userdata>value</userdata>的值
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 /** 2 * @Title Row.java 3 * @Package 4 * @author 5 * @date 2015-10-12 上午11:06:34 6 * @version V1.0 7 */ 8 9 import java.util.List; 10 11 /** 12 * @ClassName Row 13 * @author 14 * @date 2015-10-12 上午11:06:34 15 */ 16 public class Row { 17 private List<Cell> cells ; 18 private String id=""; 19 private String xmlkids=""; 20 /** 21 * @return the xmlkids 22 */ 23 public String getXmlkids() { 24 return xmlkids; 25 } 26 /** 27 * @param xmlkids the xmlkids to set 28 */ 29 public void setXmlkids(String xmlkids) { 30 this.xmlkids = xmlkids; 31 } 32 /** 33 * @return the id 34 */ 35 public String getId() { 36 return id; 37 } 38 /** 39 * @param id the id to set 40 */ 41 public void setId(String id) { 42 this.id = id; 43 } 44 private List<UserData> userDatas; 45 /** 46 * @return the userDatas 47 */ 48 public List<UserData> getUserDatas() { 49 return userDatas; 50 } 51 /** 52 * @param userDatas the userDatas to set 53 */ 54 public void setUserDatas(List<UserData> userDatas) { 55 this.userDatas = userDatas; 56 } 57 /** 58 * @return the cells 59 */ 60 public List<Cell> getCells() { 61 return cells; 62 } 63 /** 64 * @param cells the cells to set 65 */ 66 public void setCells(List<Cell> cells) { 67 this.cells = cells; 68 } 69 /* (non-Javadoc) 70 * @see java.lang.Object#toString() 71 */ 72 @Override 73 public String toString() { 74 return "Row [cells=" + cells + ", id=" + id + ", userDatas=" + userDatas 75 + ", xmlkids=" + xmlkids + "]"; 76 } 77 78 }
private List<Cell> cells ;
private String id="";
private String xmlkids="";
该行对象当中存放的是cells,也就是单个单元格,id表示当前行的id,xmlkids表示是否有子节点,1表示有子节点
Rows.java
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 /** 2 * @Title Rows.java 3 * @Package 4 * @author 5 * @date 2015-10-12 上午11:09:47 6 * @version V1.0 7 */ 8 9 10 import java.util.List; 11 12 /** 13 * @ClassName Rows 14 * @author 15 * @date 2015-10-12 上午11:09:47 16 */ 17 public class Rows { 18 /** 19 * @return the rows 20 */ 21 private List<Row> rows; 22 private String parent=""; 23 public List<Row> getRows() { 24 return rows; 25 } 26 /** 27 * @param rows the rows to set 28 */ 29 public void setRows(List<Row> rows) { 30 this.rows = rows; 31 } 32 /** 33 * @return the parent 34 */ 35 public String getParent() { 36 return parent; 37 } 38 /** 39 * @param parent the parent to set 40 */ 41 public void setParent(String parent) { 42 this.parent = parent; 43 } 44 /* (non-Javadoc) 45 * @see java.lang.Object#toString() 46 */ 47 @Override 48 public String toString() { 49 return "Rows [parent=" + parent + ", rows=" + rows + "]"; 50 } 51 52 53 }
private List<Row> rows;
private String parent="";
多行对象当中存放的是List<Row>对象,表示的是存放的是多行,parent表示的是父节点的ID,和row当中的id相关
TreeGrid.java
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 /** 2 * @Title TreeGrid.java 3 * @Package 4 * @author 5 * @date 2015-10-12 上午11:16:58 6 * @version V1.0 7 */ 8 9 10 11 import java.util.ArrayList; 12 import java.util.List; 13 14 import org.slf4j.Logger; 15 import org.slf4j.LoggerFactory; 16 17 18 /** 19 * @ClassName TreeGrid 20 * @author 21 * @date 2015-10-12 上午11:16:58 22 */ 23 public class TreeGrid { 24 private String xmlHeader = XMLUtils.getXmlHeader(); 25 private Logger logger = LoggerFactory.getLogger(this.getClass()); 26 27 /** 28 * @return the xmlHeader 29 */ 30 public String getXmlHeader() { 31 return xmlHeader; 32 } 33 34 /** 35 * @param xmlHeader 36 * the xmlHeader to set 37 */ 38 public void setXmlHeader(String xmlHeader) { 39 this.xmlHeader = xmlHeader; 40 } 41 42 /** 43 * 转换成xml文件输出 44 * 45 * @param rows 46 * @return String 47 * @author 48 * @date 2015-10-12 上午11:21:18 49 */ 50 public String asXml(Rows rows) { 51 StringBuffer stringBuffer = new StringBuffer(); 52 stringBuffer.append(this.xmlHeader); 53 stringBuffer.append("<rows"); 54 // 设置parent 55 String parent = rows.getParent(); 56 if (!"".equals(parent) && rows.getParent() != null) { 57 stringBuffer.append(" parent="); 58 stringBuffer.append("'"); 59 stringBuffer.append(rows.getParent()); 60 stringBuffer.append("'"); 61 stringBuffer.append(">"); 62 } else { 63 stringBuffer.append(">"); 64 } 65 // 遍历rows 也就是拼接row 66 if (rows.getRows() != null) { 67 for (int i = 0; i < rows.getRows().size(); i++) { 68 // 遍历cells 拼接cell,userDate 69 Row cells = rows.getRows().get(i); 70 // 拼接cell 71 stringBuffer.append(this.appendCells( cells)); 72 } 73 }else 74 { 75 stringBuffer.append(">"); 76 } 77 // 最后拼接上 78 stringBuffer.append("</rows>"); 79 logger.debug("ROWS"); 80 logger.debug(stringBuffer.toString()); 81 return stringBuffer.toString(); 82 } 83 84 /** 85 * 拼接cells 86 * @param stringBuffer 87 * @param cells 88 * @return StringBuffer 89 * @author 90 * @date 2015-10-12 下午01:58:55 91 */ 92 private StringBuffer appendCells(Row cells) 93 { 94 // 拼接cell 95 StringBuffer stringBuffer = new StringBuffer(); 96 stringBuffer.append("<row"); 97 //拼装属性xmlKids 98 if(!"".equals(cells.getXmlkids())&&cells.getXmlkids()!=null) 99 { 100 stringBuffer.append(" xmlkids="); 101 stringBuffer.append("'"); 102 stringBuffer.append(cells.getXmlkids()); 103 stringBuffer.append("'"); 104 } 105 //拼装属性id 106 if(!"".equals(cells.getId())&&cells.getId()!=null) 107 { 108 stringBuffer.append(" id="); 109 stringBuffer.append("'"); 110 stringBuffer.append(cells.getId()); 111 stringBuffer.append("'"); 112 stringBuffer.append(">"); 113 }else 114 { 115 stringBuffer.append(">"); 116 } 117 for (int c = 0; c < cells.getCells().size(); c++) { 118 // 拼接cells 119 stringBuffer.append("<cell "); 120 if (!"".equals(cells.getCells().get(c).getImage()) 121 && cells.getCells().get(c) != null) { 122 stringBuffer.append(" image="); 123 stringBuffer.append("'"); 124 stringBuffer.append(cells.getCells().get(c).getImage()); 125 stringBuffer.append("'"); 126 stringBuffer.append(">"); 127 } else { 128 stringBuffer.append(">"); 129 } 130 stringBuffer.append("<![CDATA["); 131 stringBuffer.append(cells.getCells().get(c).getValue()); 132 stringBuffer.append("]]>"); 133 stringBuffer.append("</cell>"); 134 } 135 //拼接userdata 136 stringBuffer.append(this.appendUserData(cells)); 137 stringBuffer.append("</row>"); 138 logger.debug("CELLS"); 139 logger.debug(stringBuffer.toString()); 140 return stringBuffer; 141 } 142 143 /** 144 * 拼接userData 145 * @param stringBuffer 146 * @param cells 147 * @return StringBuffer 148 * @author 149 * @date 2015-10-12 下午01:59:07 150 */ 151 private StringBuffer appendUserData(Row cells) 152 { 153 // 拼接userDate 154 StringBuffer stringBuffer = new StringBuffer(); 155 if (cells.getUserDatas()!=null&&!cells.getUserDatas().isEmpty()) { 156 for (int i = 0; i < cells.getUserDatas().size(); i++) { 157 stringBuffer.append("<userdata"); 158 stringBuffer.append(" name="); 159 stringBuffer.append("'"); 160 stringBuffer.append(cells.getUserDatas().get(i).getName()); 161 stringBuffer.append("'"); 162 stringBuffer.append(">"); 163 stringBuffer.append(cells.getUserDatas().get(i).getValue()); 164 stringBuffer.append("</userdata>"); 165 } 166 } 167 logger.debug("USERDATA"); 168 logger.debug(stringBuffer.toString()); 169 return stringBuffer; 170 } 171 172 /** 173 * 174 * @param args void 175 * @author 176 * @date 2015-10-12 下午03:40:46 177 */ 178 public static void main(String[] args) { 179 180 /* 181 * <?xml version='1.0' encoding='utf-8'?> 182 <rows> 183 <row id='000125103260000300084800' xmlkids='1'> 184 <cell image='2510.gif'>OLT设备</cell> 185 <cell> 186 <![CDATA[HCHC009/Z01/OLT193744 ]]> 187 </cell> 188 </row> 189 </rows> 190 */ 191 192 193 /* 194 * <?xml version='1.0' encoding='utf-8'?> 195 <rows parent='000125103260000300084800'> 196 <row id='000103052220000300207475' xmlkids='1'> 197 <cell image='305.gif'>机框</cell> 198 <cell> 199 <![CDATA[01]]> 200 </cell> 201 <userdata name='canRefresh'>true</userdata> 202 </row> 203 </rows> 204 */ 205 206 TreeGrid treeGrid = new TreeGrid(); 207 Rows rows = new Rows(); 208 rows.setParent("000125103260000300084800"); 209 List<Row> rowss = new ArrayList<Row>(); 210 Row e = new Row(); 211 e.setId("000125103260000300084800"); 212 UserData userdate = new UserData(); 213 userdate.setName("canRefresh"); 214 userdate.setValue("true"); 215 List<UserData> listUserData = new ArrayList<UserData>(); 216 listUserData.add(userdate); 217 e.setUserDatas(listUserData); 218 List<Cell> cells = new ArrayList<Cell>(); 219 Cell cell1 = new Cell(); 220 cell1.setImage("o.gif"); 221 cell1.setValue("01"); 222 Cell cell2 = new Cell(); 223 cell2.setImage("o.gif"); 224 cell2.setValue("机框"); 225 cells.add(cell2); 226 cells.add(cell1); 227 e.setCells(cells); 228 rowss.add(e); 229 rows.setRows(rowss); 230 System.out.println(treeGrid.asXml(rows)); 231 } 232 }
private String xmlHeader = XMLUtils.getXmlHeader(); 表示的xml的头部信息
<?xml version='1.0' encoding='utf-8'?>
方法public String asXml(Rows rows)用来将组装好的数据展现出来,只要符合了API当中的组成方式,那么就可以在前台展示了。