一个简单的树型XML文档的生成
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;
import com.shinecode.cybertraining.domain.Chapter;
/**
* 类名:CreateTreeXml.java
* 版本:
* 更新日期:2008-4-24
* 作者:Java.newlife
* 功能描述:生成树型目录的XML文档
*/
public class CreateTreeXml {
private Element root = new Element("nodes");
private Document doc = new Document(root);
private String path = null;
public CreateTreeXml(){
}
public CreateTreeXml(String path) {
this.path = path;
}
/*创建树的入口*/
private void start(List<Tree> list)
{
Element e = null;
createFile(0,e,list);
}
//要注意一下的,生成节点时
private void createFile(int PId,Element p, List<Tree> list)
{
if(p == null) p = root;
StringBuffer sb = new StringBuffer();
for(int i = 0;i<list.size();i++)
{
Tree temp = (Tree) list.get(i);
//创建当前节点对象
Element nowNode = new Element("node");
/* 存入ID时的规则(1.只用当前ID;2.用父ID+当前ID)*/
sb.delete(0, sb.length());
// sb.append(temp.getParentId());
sb.append(temp.getId());
nowNode.setAttribute("id",sb.toString());
nowNode.setAttribute("text",temp.getTitle());
// 找到指定级别的子类
if (PId == temp.getParentId())
{
// 这里是打印,可以换成输出,或是做别的处理
System.out.println(temp.getTitle() + " parentId : " + PId);
p.addContent(nowNode); //加入当前节点到父节点
// 判断是否存在下级节点,若存在读取所有节点
if (hasChild(list, temp.getId()))
createFile(temp.getId(),nowNode,list);
}
}
saveXmlFile();
}
/*把数据写入文件*/
private void saveXmlFile()
{
XMLOutputter xmlout = new XMLOutputter();
try
{
xmlout.output(doc,new FileOutputStream(path));
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
/*是否存在子节点*/
private boolean hasChild(List<Tree> list, int parentId)
{
Iterator iterator = list.iterator();
boolean bool = false;
while (iterator.hasNext())
{
Tree t = (Tree) iterator.next();
if (parentId == t.getParentId())
{
bool = true;
break;
}
}
return bool;
}
/*生成指定内容的XML文档(在这里我是用来生成试卷考点的XML文档)*/
public void createChapterTree(){
BaseService baseService = new ChapterService();
List<Tree> treeList = new ArrayList<Tree>();
List list = baseService.findAll();
Iterator iterator = list.iterator();
while(iterator.hasNext()){
Tree tree = new Tree();
Chapter chapter = (Chapter)iterator.next();
tree.setId(chapter.getId()); //存入ID
/*是否存在父对象*/
if(chapter.getChapParentid() == null){
tree.setParentId(0);
}else{
tree.setParentId(chapter.getChapParentid().getId());
}
tree.setTitle(chapter.getChapName()); //节点名称
treeList.add(tree);
}
start(treeList);
}
public static void main(String[] arg){
/*测试生成XML文件开始*/
CreateTreeXml ctx = new CreateTreeXml("Chapter.xml");
ctx.createChapterTree();
}
/**
* @return the path
*/
public String getPath() {
return path;
}
/**
* @param path the path to set
*/
public void setPath(String path) {
this.path = path;
}
}
Tree.java 是一个简单的JavaBean,有四个属性:
int id;//数据ID
int parentId;//父ID
String title;标题
int dept;//节点深度
(这里可以添加更多的属性,如:链接...)
页面显示树我用的MzTreeView2.0 ,加载生成的这个XML文件即可。。。