public class Node { private int id; private String code; private String name; private int pid; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getPid() { return pid; } public void setPid(int pid) { this.pid = pid; } } public static void tree(List<Node> list,StringBuffer sb,int pid){ for (int i = 0; i < list.size(); i++) { Node temp = list.get(i); if(temp.getPid()==pid){ sb.append("<item id='").append(temp.getId()).append("' text='").append(temp.getName()).append("'>\n"); list.remove(i); i--; tree(list, sb, temp.getId()); sb.append("</item>\n"); } } }
public static void main(String[] args) {
List<Node> list = new ArrayList<Node>(); Node node = new Node(); node.setCode("AA"); node.setId(1); node.setPid(0); list.add(node); /*省略了数据*/
StringBuffer sb = new StringBuffer(); tree(list, sb, 0); System.out.println(sb.toString()); }
一时兴起,用JDOM做了这个作业,LZ参考参考吧 Document doc = new Document(); Element root = new Element("tree"); doc.setRootElement(root); while (rs.next()) { Element current = root; int pid = rs.getInt("pid"); if (pid != 0) { XPath xp = XPath.newInstance("//*[@id=" + pid + "]"); current = (Element) xp.selectNodes(doc).get(0); } Element e = new Element("item"); e.setAttribute("id", String.valueOf(rs.getInt("id"))); e.setAttribute("text", rs.getString("name")); current.addContent(e); } Format f = Format.getPrettyFormat(); f.setIndent(" "); f.setEncoding("UTF-8"); XMLOutputter p = new XMLOutputter(f); p.output(doc, new OutputStreamWriter(new FileOutputStream( "result.xml"), "UTF-8"));代码中rs为从数据库查到的ResultSet,文本结果输出到result.xml文件中了。
1. 首先当然是查出root下的节点
2. 遍历所有节点下的节点.
3. 一层一层的子节点添加到父节点下.
public class Node {
private int id;
private String code;
private String name;
private int pid;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
} public static void tree(List<Node> list,StringBuffer sb,int pid){
for (int i = 0; i < list.size(); i++) {
Node temp = list.get(i);
if(temp.getPid()==pid){
sb.append("<item id='").append(temp.getId()).append("' text='").append(temp.getName()).append("'>\n");
list.remove(i);
i--;
tree(list, sb, temp.getId());
sb.append("</item>\n");
}
}
}
public static void main(String[] args) {
List<Node> list = new ArrayList<Node>();
Node node = new Node();
node.setCode("AA");
node.setId(1);
node.setPid(0);
list.add(node);
/*省略了数据*/
StringBuffer sb = new StringBuffer();
tree(list, sb, 0);
System.out.println(sb.toString());
}
Element root = new Element("tree");
doc.setRootElement(root);
while (rs.next()) {
Element current = root;
int pid = rs.getInt("pid");
if (pid != 0) {
XPath xp = XPath.newInstance("//*[@id=" + pid + "]");
current = (Element) xp.selectNodes(doc).get(0);
}
Element e = new Element("item");
e.setAttribute("id", String.valueOf(rs.getInt("id")));
e.setAttribute("text", rs.getString("name"));
current.addContent(e);
}
Format f = Format.getPrettyFormat();
f.setIndent(" ");
f.setEncoding("UTF-8");
XMLOutputter p = new XMLOutputter(f);
p.output(doc, new OutputStreamWriter(new FileOutputStream(
"result.xml"), "UTF-8"));代码中rs为从数据库查到的ResultSet,文本结果输出到result.xml文件中了。