java制造xml树图
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);
}
}
表结构在压缩包里。