当前位置: 技术问答>java相关
JDOM的问题
来源: 互联网 发布时间:2015-09-25
本文导语: 如何将一个document添加,删除element以及给element添加,删除属性,然后保存成xml?哪个dx能否写个例程? | 添加删除属性可以参考jdom的api很简单 import org.jdom.*; import java.io.*; import org.jdom.output....
如何将一个document添加,删除element以及给element添加,删除属性,然后保存成xml?哪个dx能否写个例程?
|
添加删除属性可以参考jdom的api很简单
import org.jdom.*;
import java.io.*;
import org.jdom.output.*;
public class myxml
{
public static void main(String[] args)
{
System.out.println("Hello World!");
Element mail=new Element("mail");
Document doc=new Document(mail);
org.jdom.Comment com=new Comment("电子邮件 XML");
doc.addContent(com);
Element from=new Element("from");
Element to=new Element("to");
Element subject=new Element("subject");
Element date=new Element("date");
Element content=new Element("content");
from.addContent("发信人");
to.addContent("收信人");
subject.addContent("主题");
date.addContent("日期");
content.addContent("内容");
mail.addContent(from);
mail.addContent(to);
mail.addContent(subject);
mail.addContent(date);
mail.addContent(content);
XMLOutputter outputter=null;
try {
outputter = new XMLOutputter(" ", true);
outputter.setEncoding("GB2312");
outputter.output(doc, System.out);
}
catch (java.io.IOException e) {
e.printStackTrace();
}
try {
FileWriter writer = new FileWriter("mail.xml");
outputter.output(doc, writer);
writer.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
import org.jdom.*;
import java.io.*;
import org.jdom.output.*;
public class myxml
{
public static void main(String[] args)
{
System.out.println("Hello World!");
Element mail=new Element("mail");
Document doc=new Document(mail);
org.jdom.Comment com=new Comment("电子邮件 XML");
doc.addContent(com);
Element from=new Element("from");
Element to=new Element("to");
Element subject=new Element("subject");
Element date=new Element("date");
Element content=new Element("content");
from.addContent("发信人");
to.addContent("收信人");
subject.addContent("主题");
date.addContent("日期");
content.addContent("内容");
mail.addContent(from);
mail.addContent(to);
mail.addContent(subject);
mail.addContent(date);
mail.addContent(content);
XMLOutputter outputter=null;
try {
outputter = new XMLOutputter(" ", true);
outputter.setEncoding("GB2312");
outputter.output(doc, System.out);
}
catch (java.io.IOException e) {
e.printStackTrace();
}
try {
FileWriter writer = new FileWriter("mail.xml");
outputter.output(doc, writer);
writer.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}