不知道,我只知道怎么用jdom读取xml文件,要写一个xml文件就不怎么清楚了呵呵,但是我觉得你这个方法好像不是很好啊,为什么要把有的写入xml文件存取呢,直接插入到数据库不是很直接和方便么?
解决方案 »
- 怎么把j2ee的工程打包成war啊?
- 碰到这样一个问题:比如说我要找回我的用户密码,通过密码问题一步一步的完成之后,最后要将找回的密码发送到用户邮箱中去,请问怎么实现?
- 如果您能回答这些问题,就可以去sun公司了——高手请进
- 新手请教一个关于EJB部署描述的问题??
- 【急】:『问关于struts中logic的用法』
- TOMCAT配置已经成功,但我如何测试我自己的JSP页面?
- 关于struts中formbean的问题
- 请教高手:JAVA面试问题
- 求代码(超简单) 狂散分!
- springmvc中如何使用ajax 求大神指导 谢谢
- 急需一个发SOAP请求的程序
- 有人研究过Tomcat5.0.X版本下的server.xml配置文件吗?
import org.jdom.input.*;
import org.jdom.output.*;
import java.io.*;public class rw_xml { public static void main(String args[]) {
try {
SAXBuilder sb = new SAXBuilder();
//创建文档
Document doc = sb.build(new FileInputStream("E:\\a.xml"));
//获得这个文档得跟元素
Element el = doc.getRootElement();
//获得这个跟元素,的所有子元素
java.util.List ls = el.getChildren();
//得到第一个子元素
Element book = (Element) ls.get(0);//获得这个元素的子元素,(指定)
Element el2 = book.getChild("listst");
//输出这个元素的值
System.out.println(el2.getName());
//给这个元素的值改个名字
el2.setText("cute");//再指定元素获得这个值
Element el3 = book.getChild("list");
String a = "";
boolean bool = true;
XMLOutputter xml = new XMLOutputter(a, bool, "gb2312");
xml.output(doc, new FileOutputStream("E:\\cute.xml"));
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
}根据这个,来试试
<list>
<id>1</id>
<username>冯奎</username>
<time>01-4-23 18:26:56</time>
<homepage>http://www.getjob.com.cn</homepage>
<email>[email protected]</email>
<context>我的留言</context>
</list>
</lists>
这个如何增加到a.xml中
File fn = new File(filename);
Document doc = null;
SAXBuilder sb = new SAXBuilder();
if(fn.exists()){
try{
doc = sb.build(new FileInputStream(filename));
}catch(Exception e){
System.out.println(e.getMessage());
}
Element root = doc.getRootElement();
//List list_two = root.getChildren();
//Element el_two = root.getChild("two");
Element el_two = root.getChild(element);
if(root.getContentSize()>0){
int index = root.indexOf(el_two);
Element el_three = new Element("three");
el_three.addContent("three");
el_three.setAttribute("memo","new add");
root.addContent(index+1,el_three);
//
//设置输出格式
Format f = Format.getRawFormat();
f.setIndent(" ");
f.setEncoding("GBK");
XMLOutputter output = new XMLOutputter(f);
try{
output.output(doc, new FileOutputStream(filename));
}catch(Exception e){
System.out.println(e.getMessage());
}
}else{
System.out.println("Not found child element!");
}
}else{
System.out.println("找不到指定的文件!");
}
}
public static void main(String[] args) {
//修改XML文件
operateXML1.EditXML("output.xml","one");
}
output.xml文件的内容:
<?xml version="1.0" encoding="GBK"?>
<root name="root">
<one>one</one>
<three memo="new add">three</three>
<two name="two">two</two>
<!--This is a test!-->
</root>
楼主稍加改造应该能实现楼主的要求!
<?xml version="1.0" encoding="GBK"?>
<root>
<one>one</one>
<two name="two">two</two>
<!--This is a test!-->
</root>
<root>
<one>three</one>
<four name="four">four</four>
<!--This is a test!-->
</root>
<root>
<one>five</one>
<six name="six">two</six>
<!--This is a test!-->
</root>
List list_two = root.getChildren();不就行了
比如前面
<?xml version="1.0" encoding="GBK"?>
<root>
<one>one</one>
<two name="two">two</two>
<!--This is a test!-->
</root>我要在xml文件下面接着增加
<root>
<one>three</one>
<four name="four">four</four>
<!--This is a test!-->
</root>
<root>
<one>five</one>
<six name="six">two</six>
<!--This is a test!-->
</root>
等等信息