救急 关于JDOM生成XML的问题 小弟使用JDOM 生成一个XML 文件中间有一个标签如<BNIF seqno="1">这样,后面是一个属性 标识该字段在本文件中的编号 如seqno="1",seqno="2",seqno="3"这样,请各位大大指点这个属性用JDOM 的什么方法写入 完成就结帖 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 addAttribute("属性名","值") ; Jdom的用法:import java.io.FileOutputStream;import java.io.IOException;import java.util.ArrayList;import java.util.List;import org.jdom.Document;import org.jdom.Element;import org.jdom.JDOMException;import org.jdom.input.SAXBuilder;import org.jdom.output.Format;import org.jdom.output.XMLOutputter;public class StudentDAO { String file;//xml文件的位置 FileOutputStream fo; Document doc; SAXBuilder builder = new SAXBuilder(); Format format; //用于设置xml文件的格式 private StudentDAO() { } //构造函数中设置file和format public StudentDAO(String webroot) { file = webroot + "/data/student.xml"; format = Format.getPrettyFormat(); format.setIndent(" "); format.setEncoding("utf-8"); } //向xml文件中添加一条记录 public boolean add(StudentInfo si) { String id = si.getId(); boolean idAlreadyExisted = false; boolean addResult = false; Element root; List allStudent; try { doc = builder.build(file); root = doc.getRootElement(); allStudent = root.getChildren(); //检查要添加的信息是否存在 for (int i = 0; i < allStudent.size(); i++) { Element element = (Element) allStudent.get(i); if (element.getChild("id").getText().equals(id)) { idAlreadyExisted = true; break; } } //向xml文件中添加新的信息,并返回true if (!idAlreadyExisted) { Element student = new Element("student"); student.addContent(new Element("id").setText(si.getId())); student.addContent(new Element("name").setText(si.getName())); student.addContent(new Element("birthday").setText(si .getBirthday())); student.addContent(new Element("address").setText(si .getAddress())); allStudent.add(student); XMLOutputter xout = new XMLOutputter(format); fo = new FileOutputStream(file); xout.output(doc, fo); return true; } } catch (JDOMException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { close(); } return addResult; } //从xml文件中删除一条信息 public boolean del(String id) { boolean delResult = false; Element root; List allStudent; try { doc = builder.build(file); root = doc.getRootElement(); allStudent = root.getChildren(); for (int i = 0; i < allStudent.size(); i++) { Element student = (Element) allStudent.get(i); //如果id存在,则删除些条信息,并返回true if (student.getChild("id").getText().equals(id)) { allStudent.remove(i); XMLOutputter xout = new XMLOutputter(format); fo = new FileOutputStream(file); xout.output(doc, fo); return true; } } } catch (JDOMException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { close(); } return delResult; } //按名字进行查询,返回所有名字以name开头的记录 public ArrayList query(String name) { ArrayList list = new ArrayList(); Element root; List allStudent; try { doc = builder.build(file); root = doc.getRootElement(); allStudent = root.getChildren(); for (int i = 0; i < allStudent.size(); i++) { Element student = (Element) allStudent.get(i); if (student.getChild("name").getText().startsWith(name)) { StudentInfo si = new StudentInfo(); si.setId(student.getChild("id").getText()); si.setName(student.getChild("name").getText()); si.setBirthday(student.getChild("birthday").getText()); si.setAddress(student.getChild("address").getText()); list.add(si); } } } catch (JDOMException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return list; } void close() { if (fo != null) { try { fo.close(); } catch (IOException e) { e.printStackTrace(); } } }} Element elem = new Element("BNIF");elem.setAttribute(new Attribute("seqno", "1")); 楼上的这个JDOM解析XML文档 不能有个普遍作用 只能用于解析 只有2级目录的 如果有3级甚至更多就行不通了 建议使用递归 求助关于jsp的数据库连接 Eclipse和 MyEclipse的区别 http 500是什么错误?指什么而言的? ireport 大牛过来看看 图片模糊 使用Datetimepicker控件为什么出现这种情况? GWT怎么调试啊? 求一在线debug客户正式系统的java代码的调试工具 如何读取属性文件,急 公司要用JAVABEAN,请推荐一,两本好书学习。谢谢 oracle的大文本问题 请知者解答!!! 在LOGJ4配置打印日志的时候,不想把JSF里的内容打印出来,该怎么办?
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
public class StudentDAO {
String file;//xml文件的位置
FileOutputStream fo;
Document doc;
SAXBuilder builder = new SAXBuilder();
Format format; //用于设置xml文件的格式
private StudentDAO() {
}
//构造函数中设置file和format
public StudentDAO(String webroot) {
file = webroot + "/data/student.xml";
format = Format.getPrettyFormat();
format.setIndent(" ");
format.setEncoding("utf-8");
}
//向xml文件中添加一条记录
public boolean add(StudentInfo si) {
String id = si.getId();
boolean idAlreadyExisted = false;
boolean addResult = false;
Element root;
List allStudent;
try { doc = builder.build(file);
root = doc.getRootElement();
allStudent = root.getChildren();
//检查要添加的信息是否存在
for (int i = 0; i < allStudent.size(); i++) {
Element element = (Element) allStudent.get(i);
if (element.getChild("id").getText().equals(id)) {
idAlreadyExisted = true;
break;
}
}
//向xml文件中添加新的信息,并返回true
if (!idAlreadyExisted) {
Element student = new Element("student");
student.addContent(new Element("id").setText(si.getId()));
student.addContent(new Element("name").setText(si.getName()));
student.addContent(new Element("birthday").setText(si
.getBirthday()));
student.addContent(new Element("address").setText(si
.getAddress()));
allStudent.add(student);
XMLOutputter xout = new XMLOutputter(format);
fo = new FileOutputStream(file);
xout.output(doc, fo);
return true;
} } catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
close();
}
return addResult;
}
//从xml文件中删除一条信息
public boolean del(String id) {
boolean delResult = false;
Element root;
List allStudent;
try {
doc = builder.build(file);
root = doc.getRootElement();
allStudent = root.getChildren();
for (int i = 0; i < allStudent.size(); i++) {
Element student = (Element) allStudent.get(i);
//如果id存在,则删除些条信息,并返回true
if (student.getChild("id").getText().equals(id)) {
allStudent.remove(i);
XMLOutputter xout = new XMLOutputter(format);
fo = new FileOutputStream(file);
xout.output(doc, fo);
return true;
}
} } catch (JDOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
close();
}
return delResult;
}
//按名字进行查询,返回所有名字以name开头的记录
public ArrayList query(String name) {
ArrayList list = new ArrayList();
Element root;
List allStudent;
try {
doc = builder.build(file);
root = doc.getRootElement();
allStudent = root.getChildren(); for (int i = 0; i < allStudent.size(); i++) {
Element student = (Element) allStudent.get(i);
if (student.getChild("name").getText().startsWith(name)) {
StudentInfo si = new StudentInfo();
si.setId(student.getChild("id").getText());
si.setName(student.getChild("name").getText());
si.setBirthday(student.getChild("birthday").getText());
si.setAddress(student.getChild("address").getText());
list.add(si);
}
}
} catch (JDOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
void close() {
if (fo != null) {
try {
fo.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
elem.setAttribute(new Attribute("seqno", "1"));
只能用于解析 只有2级目录的
如果有3级甚至更多就行不通了
建议使用递归