关于xml的问题,急!!!!!!!!!!!在线等(100分) 我在数据库中定义了三个字段(id,parentid,text),分别是节点,父节点,文本,现在我想用java程序读取数据后生成xml文件,建立树结构。急急急急!!!!!!!!!!!!!!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 递归就是方法自己调用自己,直到某个条件成立退出,非常浪费资源,我给你写个大概思路:/** * 遍历父节点pid下所有的子节点 * 参数obj是XML树的一个节点对象 */public boolean createXML(int pid,Object obj){ 根据父节点pid去数据库查父节点id都是pid的数据; String SQL="select *from table where pid="+pid; 执行SQL语句; 得到结果集rs; while(rs.next()){ int ele=rs.getInt("id"); obj.add 把ele加为obj的子节点; 用递归方法调用自己,把刚取到的id和新加的子节点传进去; createXML(ele,obj.add新加的子节点传进去); } return true;} jdom或者dom4j都很容易实现,几分钟的事情 jdom将数据内存中, 根据父子关系遍历节点, 生成树即可 用DOM吧,发给你一段用DOM解析XML的代码: public Message[] getAllMessages() { Message returnValue[] = null; try { NodeList nl = doc.getElementsByTagName("message"); int length = nl.getLength(); returnValue = new Message[length]; for (int i = 0; i < length; i++) { NodeList messagelist = nl.item(i).getChildNodes(); returnValue[i] = new Message(); for (int j = 0; j < messagelist.getLength(); j++) { String nodeName = messagelist.item(j).getNodeName(); if (nodeName.equals("nickname")) { String nodeValue = messagelist.item(j).getFirstChild(). getNodeValue(); returnValue[i].setWriterNickname(nodeValue); } else if (nodeName.equals("writedate")) { String nodeValue = messagelist.item(j).getFirstChild(). getNodeValue(); returnValue[i].setWriteDate(nodeValue); } else if (nodeName.equals("context")) { String nodeValue = messagelist.item(j).getFirstChild(). getNodeValue(); returnValue[i].setContext(nodeValue); } } } } catch (Exception ex) { ex.printStackTrace(); } return returnValue; } java 如何取屏幕某点的像素值 与大家分享一些好资源(免费电子书和视频教程) 简单问题。。求教。。 linux下jspsmartupload上传表单中文乱码 求助:关于InputStream ◆◆在JBuilder下,为什么一单步执行程序,光标就进入到.class文件里了?◆◆ 类继承问题。。。。 Applet 如何从服务器上读写文件? 有没有JCertify5.0下载? 请教一个对Calender赋值的问题 ActionEvent 问题 附代码 by af_corps 问个弱智问题.
/**
* 遍历父节点pid下所有的子节点
* 参数obj是XML树的一个节点对象
*/
public boolean createXML(int pid,Object obj){
根据父节点pid去数据库查父节点id都是pid的数据;
String SQL="select *from table where pid="+pid;
执行SQL语句;
得到结果集rs;
while(rs.next()){
int ele=rs.getInt("id");
obj.add 把ele加为obj的子节点;
用递归方法调用自己,把刚取到的id和新加的子节点传进去;
createXML(ele,obj.add新加的子节点传进去);
}
return true;
}
public Message[] getAllMessages() {
Message returnValue[] = null;
try {
NodeList nl = doc.getElementsByTagName("message");
int length = nl.getLength();
returnValue = new Message[length];
for (int i = 0; i < length; i++) {
NodeList messagelist = nl.item(i).getChildNodes();
returnValue[i] = new Message();
for (int j = 0; j < messagelist.getLength(); j++) {
String nodeName = messagelist.item(j).getNodeName();
if (nodeName.equals("nickname")) {
String nodeValue = messagelist.item(j).getFirstChild().
getNodeValue();
returnValue[i].setWriterNickname(nodeValue);
} else if (nodeName.equals("writedate")) {
String nodeValue = messagelist.item(j).getFirstChild().
getNodeValue();
returnValue[i].setWriteDate(nodeValue);
} else if (nodeName.equals("context")) {
String nodeValue = messagelist.item(j).getFirstChild().
getNodeValue();
returnValue[i].setContext(nodeValue);
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
return returnValue;
}