使用dom4j扩展xml文件

paodan 2009-12-10 12:36:02
比如a.xml文件格式如下

<title>
<info name = "liudehua" id= "123" />
</title>


这个xml文件经常在添加新的信息的属性,但是其格式不变,比如添加<info name="limin" id="234" />等等这个的信息,如何把添加的插到原来的xml中,即

<title>
<info name = "liudehua" id= "123" />
<info name="limin" id="234" />
</title>

...全文
121 8 打赏 收藏 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
paodan 2009-12-16
  • 打赏
  • 举报
回复
结贴
苍蝇①号 2009-12-10
  • 打赏
  • 举报
回复
楼上的把方法都说了,我是来接分的
xuyang840117 2009-12-10
  • 打赏
  • 举报
回复

XMLWriter writer = null;
SAXReader saxReader = new SAXReader();
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");// 设置XML文件的编码格式
File file = new File("a.xml");
//a.xml并添加內容

InputStreamReader reader = new InputStreamReader(file, "UTF-8");
Document document = saxReader.read(reader);
Element root = document.addElement("title");//根节点
//增加节点
Element element = root.addElement("info");
element.addAttribute("name", "saxReader");//增加属性
element.addAttribute("id", "234");//增加属性

//写入文件
writer = new XMLWriter(new FileWriter(file),format);
writer.write(document);
writer.close();
paodan 2009-12-10
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 yetaodiao 的回复:]
JDOM一样

工具而已

好使就行
[/Quote]现在是接收到外部接口是dom4j的,以前没用过,所有不是很清楚,希望是dom4j的实现,然后好兼容
  • 打赏
  • 举报
回复
JDOM一样

工具而已

好使就行
paodan 2009-12-10
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 crazylaa 的回复:]
结果:
Java code

IWAV0055I Java Bean test.XMLTest started with the main method<title><info name="liudehua" id="123"/></title><title><info name="liudehua" id="123"/><info name="limin" id="234"/></title>
[/Quote]辛苦了,这么晚。我希望用dom4j实现
crazylaa 2009-12-10
  • 打赏
  • 举报
回复
结果:


IWAV0055I Java Bean test.XMLTest started with the main method
<title>
<info name="liudehua" id="123" />
</title>


<title>
<info name="liudehua" id="123" />
<info name="limin" id="234" />
</title>

crazylaa 2009-12-10
  • 打赏
  • 举报
回复


package test;

import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class XMLTest {

private String encode = "UTF-8";

private Document doc;

private Element root;

private XMLTest() {
}

public static XMLTest initXML(File file) {
XMLTest msg = new XMLTest();
try {
SAXBuilder sb = new SAXBuilder();
msg.doc = sb.build(new FileReader(file));
msg.root = msg.doc.getRootElement();
} catch (Exception e) {
msg = null;
}
return msg;
}

public boolean addNode(Element xmle) {
if (xmle.getParent() != null)
xmle.detach();
root.addContent(xmle);
return true;
}

public String toString() {
try {
XMLOutputter xmlOut = new XMLOutputter(Format.getPrettyFormat()
.setOmitDeclaration(true).setEncoding(encode));
ByteArrayOutputStream out = new ByteArrayOutputStream();
xmlOut.output(doc, out);
return new String(out.toByteArray(), encode);
} catch (IOException e) {
return null;
}
}

public static void main(String[] args) {
BufferedWriter bw = null;
try {
File f = new File("F:\\test.xml");
XMLTest xt = XMLTest.initXML(f);
System.out.println(xt.toString());
Element param = new Element("info");
param.setAttribute("name", "limin");
param.setAttribute("id", "234");
xt.addNode(param);
System.out.println(xt.toString());
bw = new BufferedWriter(new FileWriter(f, false));
bw.write(xt.toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧