1.IndustryBean.java
package com.javaXMLTree;
public class IndustryBean {
private String indusid;
private String indusname;
private String parentid;
public String getIndusid() {
return indusid;
}
public void setIndusid(String indusid) {
this.indusid = indusid;
}
public String getIndusname() {
return indusname;
}
public void setIndusname(String indusname) {
this.indusname = indusname;
}
public String getParentid() {
return parentid;
}
public void setParentid(String parentid) {
this.parentid = parentid;
}
}
2.XmlTree.java
package com.javaXMLTree;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.util.Properties;
import org.apache.crimson.tree.XmlDocument;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import com.javaXMLTree.IndustryBean;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.DriverManager;
import java.util.ArrayList;
public class XmlTree {
private static Connection conn;
private static Statement stat;
private static ResultSet rs;
//写入XML
public void WriteXML(String fileName,ArrayList list) throws Exception{
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=null;
try{
db=dbf.newDocumentBuilder();
}
catch(Exception e){
e.printStackTrace();
}
Document doc=db.newDocument();
Element root=doc.createElement("tree");
root.setAttribute("id", "0");
doc.appendChild(root);
Element item =doc.createElement("item");
item.setAttribute("text","行业选择");
item.setAttribute("open","0");
String Indusid = "";
String Indusname = "";
String Parentid = "";
Element item1 = null;
Element item2 = null;
Element item3 = null;
Element item4 = null;
for(int i=0;i<list.size();i++){
IndustryBean bean = (IndustryBean)list.get(i);
if(bean.getParentid().equals("0")){
item1=doc.createElement("item");
Indusid = bean.getIndusid();
Indusname =bean.getIndusname();
Parentid = bean.getParentid();
item1.setAttribute("text", Indusname);
item1.setAttribute("id", Indusid);
for(int j=i+1;j<list.size();j++){
IndustryBean bean2 = (IndustryBean)list.get(j);
if(j!=i+1&&bean2.getParentid().indexOf(Parentid.substring(0,1))<0){
j=list.size();
}else{
if(!bean2.getParentid().equals("0")&&bean2.getParentid().length()==1){
item2 = doc.createElement("item");
Indusid = bean2.getIndusid();
Indusname =bean2.getIndusname();
Parentid = bean2.getParentid();
item2.setAttribute("text", Indusname);
item2.setAttribute("id", Indusid);
for(int z=j+1;z<list.size();z++){
IndustryBean bean3 = (IndustryBean)list.get(z);
if(bean3.getParentid().length()==3&&bean3.getParentid().indexOf(Parentid)>=0){
item3 = doc.createElement("item");
Indusid = bean3.getIndusid();
Indusname =bean3.getIndusname();
Parentid = bean3.getParentid();
item3.setAttribute("text", Indusname);
item3.setAttribute("id", Indusid);
for(int s=z+1;s<list.size();s++){
IndustryBean bean4 = (IndustryBean)list.get(s);
if(s!=z+1&&bean4.getParentid().length()==3){
s=list.size();
}else{
if(bean4.getParentid().length()==4&&bean4.getParentid().indexOf(Parentid)>=0){
item4 = doc.createElement("item");
Indusid = bean4.getIndusid();
Indusname =bean4.getIndusname();
item4.setAttribute("text", Indusname);
item4.setAttribute("id", Indusid);
item3.appendChild(item4);
}
}
}
item2.appendChild(item3);
}
}
item1.appendChild(item2);
}
}
}
item.appendChild(item1);
}
}
/*
Element age=doc.createElement("item");
age.setAttribute("text", "飞机");
age.setAttribute("id", "fj");
Element age1=doc.createElement("item");
age1.setAttribute("text", "飞机制造");
age1.setAttribute("id", "fj1");
Element age12=doc.createElement("item");
age12.setAttribute("text", "零件制造");
age12.setAttribute("id", "fj12");
age1.appendChild(age12);
age.appendChild(age1);
Element age2=doc.createElement("item");
age2.setAttribute("text", "飞机销售");
age2.setAttribute("id", "fj2");
age.appendChild(age2);
Element age3=doc.createElement("item");
age3.setAttribute("text", "飞机维修");
age3.setAttribute("id", "fj3");
age.appendChild(age3);
item.appendChild(age);
Element tel=doc.createElement("item");
tel.setAttribute("text", "汽车");
tel.setAttribute("id", "qc");
Element tel1=doc.createElement("item");
tel1.setAttribute("text", "汽车制造");
tel1.setAttribute("id", "qc1");
Element tel12=doc.createElement("item");
tel12.setAttribute("text", "零件制造");
tel12.setAttribute("id", "qc12");
tel1.appendChild(tel12);
tel.appendChild(tel1);
Element tel2=doc.createElement("item");
tel2.setAttribute("text", "汽车销售");
tel2.setAttribute("id", "qc2");
tel.appendChild(tel2);
Element tel3=doc.createElement("item");
tel3.setAttribute("text", "汽车维修");
tel3.setAttribute("id", "qc3");
tel.appendChild(tel3);
item.appendChild(tel);
age=doc.createElement("item");
age.setAttribute("text", "飞机1");
age.setAttribute("id", "1fj");
age1=doc.createElement("item");
age1.setAttribute("text", "飞机制造1");
age1.setAttribute("id", "1fj1");
age12=doc.createElement("item");
age12.setAttribute("text", "零件制造1");
age12.setAttribute("id", "1fj12");
age1.appendChild(age12);
age.appendChild(age1);
age2=doc.createElement("item");
age2.setAttribute("text", "飞机销售1");
age2.setAttribute("id", "1fj2");
age.appendChild(age2);
age3=doc.createElement("item");
age3.setAttribute("text", "飞机维修1");
age3.setAttribute("id", "1fj3");
age.appendChild(age3);
item.appendChild(age);
*/
root.appendChild(item);
try{
FileOutputStream fos=new FileOutputStream(fileName);
OutputStreamWriter osw=new OutputStreamWriter(fos,"UTF-8");
((XmlDocument)doc).write(osw,"utf-8");
osw.close();
fos.close();
}
catch(Exception e){
e.printStackTrace();
}
}
public ArrayList GetCollocate() throws IOException{
Properties initProps = new Properties();
InputStream in = null;
int count = 0;
try {
in =getClass().getResourceAsStream("config.properties");
initProps.load(in);
Class.forName(initProps.getProperty("DBDriver"));
conn = DriverManager.getConnection(initProps.getProperty("DBUrl"), initProps.getProperty("DBUser"), initProps.getProperty("DBPassword"));
stat = conn.createStatement();
rs = stat.executeQuery("select * from Industry where Parentid in (select distinct Parentid from Industry order by Parentid) order by Indusid ");
ArrayList<IndustryBean> list =new ArrayList<IndustryBean>();
while(rs.next()){
IndustryBean bean = new IndustryBean();
bean.setIndusid(rs.getString("indusid"));
bean.setIndusname(rs.getString("indusname"));
bean.setParentid(rs.getString("parentid"));
list.add(bean);
count++;
}close(rs);
return list;
} catch (Exception e) {
System.out.println("读系统属性\"config.properties\"失败");
e.printStackTrace();
}
return null;
}
public static void ConnectDataBase(){
try {
} catch (Exception e) {
e.printStackTrace();
}finally{
}
}
public static void close(Connection conn) {
try {
if (conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(PreparedStatement pstmt) {
try {
if (pstmt != null) {
pstmt.close();
pstmt = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(Statement stmt) {
try {
if (stmt != null) {
stmt.close();
stmt = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(ResultSet rs) {
try {
if (rs != null) {
rs.close();
rs = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
XmlTree xml = new XmlTree();
ArrayList list = xml.GetCollocate();
xml.WriteXML("WebRoot/jsp/xmlTest.xml",list);
}
}
表结构在压缩包里。
分享到:
相关推荐
自己写的一个小项目。用数组模拟实现java的数据结构,性能还行。和源码思路比较接近。可以看看,有助于初学者了解数据结构与算法。
文件递归-XML递归-树图递归 面试中的常见递归算法:附带截图和详细代码
JAVA 生成树状图及饼图|JAVA 生成树状图及饼图|JAVA 生成树状图及饼图|JAVA 生成树状图及饼图
html页面异步请求servlet(jsp)返回json格式数据【写好的字符串】,不用连接数据库,然后在html页面生成树型图,点击伸缩或展开,公司架构,oa经常用到,没分了,要回血先,先谢谢下载的兄弟!
大数据可视化课程作业,从网上下载一个工程,用java统计工程的目录结构及代码行数,生成json文件,用D3.js绘制工程目录结构的树型结构图(矩形分区图、圆状集群图、矩阵树图)
d3.js-TreeDiagram运用d3.js框架制作的树图效果图使用方法:d3tree(第1个参数为id,第2个参数为高度,第3个参数为宽度,第4个参数为树图的第1列,第5个参数为树图的第2列,其他参数数组中第1个数为父亲节点,如第6个...
JVoroTreemap是一个快速的独立Java库,可计算Voronoi树图。 下面的文章包含与此实现有关的最重要的参考。 Arlind Nocaj,Ulrik Brandes,“计算Voronoi树图:更快,更简单,且与分辨率无关”,计算机图形学论坛,...
实现的echarts中树图点击节点收缩的js,在使用该js的时候需替换原来的echarts.js或者在原echarts.js中添加一句代码,压缩包中有引用介绍
基于echart树图实现横向组织结构图,已将连接线从曲线修改为直角线
在全面质量管理中,可以画系统图(树图)的小工具,可以以XML格式存储,便于数据交换,可以保存为图片,也可以直接输出到打印机。(如果有问题,请与我联系,mini_my@sohu.com)
本项目打包了 antvis/g6,利用 JSON 数据生成 HTML 格式的树图
图论——支撑树图论——支撑树图论——支撑树图论——支撑树图论——支撑树图论——支撑树图论——支撑树图论——支撑树图论——支撑树图论——支撑树图论——支撑树图论——支撑树图论——支撑树图论——支撑树图论...
【教程】Conflux树图区块链生态开发实用教程(合约+后端+前端+NFT,完整工程范例),适合公链的nft铸造 发行 流通销毁
这个是TreeView动态树图,我们以前用的是通过数据源绑定的,现在我们可以动态的获得TreeView结构。
基于ECharts V4.2的树图可视化设计与实现
基于echart树图实现横向组织结构图,连接线为曲线
树图例题详解,信息学竞赛的难点突破的重要内容,也是考查的重点
树图是一种非常流行的利用包含关系来表达层次化数据的可视化方法。该方法具有高效的空间利用率及良好的交互性,并广泛应用于各个领域。正方化布局算法是树图可视化方法中常用的算法,但该算法存在无序和稳定性差等...
【d3】树图-各种效果集合(附效果图)