多级树的传统作法是数据保存到数据库中,这样做的优点是可以保存的数据量大,可以用标准的sql对数据执行操作;但是这样做的缺点也是很明显的,那就是层次结构不能得到很好的体现,很不直观,维护起来比较困难;在数据量不是很大的情况下(如管理后台左边的导航栏数据),我比较喜欢把数据保存在 xml文件中,节点间的层次结构得到很好的体现,同时在增加一个节点的时候不再要求一定要同时该节点的父亲节点的信息;系统的结构为Ext2.0+struts1.3.8+spring2.0;有兴趣的朋友在下载后,将项目发布到Tomcat,在浏览器中输入http://localhost:8080/xmlTree/Province_City_County/provinceCityCounty_xml.html即可看到效果。以下为主要代码(红色部分为核心代码
)
实体类
package entity;
import java.util.List;
public class XmlTreeNode {
public String treeId; /*树节点id*/
public String text; /*树节点文本值*/
public List children; /*树节点对应孩子节点*/
public List getChildren() {
return children;
}
public void setChildren(List children) {
this.children = children;
}
public String getTreeId() {
return treeId;
}
public void setTreeId(String treeId) {
this.treeId = treeId;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
action对应代码
package action;
import java.io.PrintWriter;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import service.XmlTreeService;
import util.Factory;
import util.Print;
public class PCC_Xml_DataAction extends Action {
private XmlTreeService xmlTreeService;
private PrintWriter out ;
public ActionForward execute(ActionMapping mapping,ActionForm form ,HttpServletRequest request,HttpServletResponse response)throws Exception{
response.setContentType("text/x-json;charset=UTF-8");
xmlTreeService=(XmlTreeService) Factory.getBean("xmlTreeService");
String json=xmlTreeService.getResponseJson();
out=response.getWriter();
out.println(json);
out.flush();
out.close();
return null;
}
}
业务层实现类
package service;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import entity.XmlTreeNode;
import util.Factory;
import util.Print;
import util.Tools;
import util.XmlTreeTool;
public class XmlTreeServiceImpl implements XmlTreeService {
/**获得一个Element获得一个集合,该集合的元素为该元素的子Elemtn转化而成的对象*/
public List getChildrens(Element element) {
List resultList=new ArrayList();
NodeList childrenNodes=element.getChildNodes();
for(int i=0;i<childrenNodes.getLength();i++){
if(childrenNodes.item(i).getNodeType()==1){
Element tempChildren=(Element) childrenNodes.item(i);
XmlTreeNode tempNode=elementToNode(tempChildren);
List tempList=getChildrens(tempChildren);
if(tempList.size()!=0){
tempNode.setChildren(tempList);
}
resultList.add(tempNode);
}
}
return resultList;
}
/**获得根元素的子元素转化而成的对象列表*/
public List getRootEleChild(){
DocumentBuilder documentBuilder=Factory.getDocumentBuilder();
Element rootElement=null;
try {
Document doc=documentBuilder.parse(XmlTreeServiceImpl.class.getClassLoader().getResourceAsStream("province.xml"));
rootElement=doc.getDocumentElement();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return getChildrens(rootElement);
}
/** 实现从Element到XmlTreeNode的转化*/
public XmlTreeNode elementToNode(Element element){
XmlTreeNode result=new XmlTreeNode();
boolean textIsNull=false;
String treeId=element.getAttribute("id");
String treeName1=element.getAttribute("text");
if(!Tools.isNull(treeId)){
result.setTreeId(treeId);
}
if(!Tools.isNull(treeName1)){
result.setText(treeName1);
return result;
}
String treeName2=element.getFirstChild().getNodeValue();
if(!Tools.isNull(treeName2)){
result.setText(treeName2);
}
return result;
}
public String getResponseJson(){
String json=Tools.listToJson(getRootEleChild())+"";
return transferJson(json);
}
/**把输入的数据转化成最终形式(client端想要的Json格式)*/
public String transferJson(String input){
String result=input.replaceAll("\"children\":\\[\\],","leaf:true,");
return result;
}
}
前台页面代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>provinceCityCounty.html</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="../scripts/ext/resources/css/ext-all.css"/>
<script type="text/javascript" src="../scripts/ext/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="../scripts/ext/ext-all.js"></script>
<script type="text/javascript">
Ext.onReady(function(){
var tree=new Ext.tree.TreePanel({
renderTo:'pcc_xml',
width:1000,
rootVisible:false,
loader:new Ext.tree.TreeLoader({
dataUrl:'../pCC_Xml_Data.do'
}),
root:new Ext.tree.AsyncTreeNode({
text:'树的根'
})
});
tree.render();
});
</script>
</head>
<body>
<div id="pcc_xml"></div>
</body>
</html>
分享到:
相关推荐
12.8.2 如何使用XML DOM--一个利用XML实现多级关联下拉选择框的例子 12.9 总结 第13章 事件处理 13.1 什么是事件 13.1.1 消息与事件响应 13.1.2 浏览器的事件驱动机制 13.2 基本事件处理 ...
实例051 利用ListBox控件实现标签式数据选择 实例052 在ListBox控件间实现数据交换 实例053 列表项的提示条 2.4 ComboBox控件典型实例 实例054 将数据表中的字段添加到ComboBox控件 实例055 带查询功能的...
实例051 利用ListBox控件实现标签式数据选择 实例052 在ListBox控件间实现数据交换 实例053 列表项的提示条 2.4 ComboBox控件典型实例 实例054 将数据表中的字段添加到ComboBox控件 实例055 带查询功能的...
Asp.Net(C#)利用XPath解析XML文档示例 XSL .Net框架下的XSLT转换技术简介 一个XSLT的简单例子 XSLXSLT板主题整理 xsl入门的好文章 新手学习XSL的好东西 XSL语法介绍 XSL学习心得 - 调用属性值 XSLT与XML转换的详细...
cc实例178 内存使用状态 cc实例179 监视剪贴板内容 cc实例180 利用钩子技术实现键盘监控 6.6 程序相关设置 cc实例181 用列表显示系统正在运行的程序 cc实例182 为程序添加快捷方式 cc实例183 设置其他...
cc实例051 利用ListBox控件实现标签式数据选择 cc实例052 在ListBox控件间实现数据交换 cc实例053 列表项的提示条 2.4 ComboBox控件典型实例 cc实例054 将数据表中的字段添加到ComboBox控件...
cc实例051 利用ListBox控件实现标签式数据选择 cc实例052 在ListBox控件间实现数据交换 cc实例053 列表项的提示条 2.4 ComboBox控件典型实例 cc实例054 将数据表中的字段添加到ComboBox控件...
23、强健的模块、频道、栏目、专题、信息的权限分配,利用本系统可实现多种用户类型的浏览、发布、管理等权限,让打造多管理模式的大型网站不再成为难题。 新增及升级功能列表: 1.修补用户中心和后台管理中部分...
23、强健的模块、频道、栏目、专题、信息的权限分配,利用本系统可实现多种用户类型的浏览、发布、管理等权限,让打造多管理模式的大型网站不再成为难题。 默认管理员(前/后台):admin 密码:admin888 初始认证...