import org.jdom.*;
import org.jdom.output.*;
import java.io.*;
import java.util.list;
/**
* <p>title: 使用jdom操作xml文件。</p>
* <p>description: </p>
* <p>copyright: copyright (c) 2003</p>
* <p>filename: jdomoption.java</p>
* @version 1.0
*/
public class jdomoption {
/**
*<br>方法说明:构造器,实现构造xml文件,删除元素
*<br>输入参数:
*<br>返回类型:
*/
public jdomoption() {
string[] strchapter = { "基础知识", "图形界面", "图形处理", "i/o操作", "网络编程", "数据库" };
element elmtroot = new element("article");
document docjdom = new document(elmtroot);
//新建元素
for(int i=0; i<6; i++) {
//声明新元素
element elmtchapter = new element("chapter");
//添加内容
elmtchapter.addcontent(strchapter[i]);
//构造属性,并添加到元素中
attribute a=new attribute("sort",new integer(i).tostring());
elmtchapter.addattribute(a);
//将元素添加到根节点下
elmtroot.addcontent(elmtchapter);
}
//?h除第4个元素
list lstchapter = elmtroot.getchildren("chapter");
lstchapter.remove(4);
//??出
outputxml(docjdom, "myjdom.xml");
}
/**
*<br>方法说明:输出xml文件
*<br>输入参数:document docxml xml文件内容
*<br>输入参数:string strfilename 输出文件名称
*<br>返回类型:
*/
private void outputxml(document docxml, string strfilename) {
//使用jdom的xml输出
xmloutputter fmt = new xmloutputter();
try {
//声明使用gb2312字符集
fmt.setencoding("gb2312");
//可以换行
fmt.setnewlines(true);
//输出文件对象
filewriter fwxml = new filewriter(strfilename);
fmt.output(docxml, fwxml);
fwxml.close();
}catch (ioexception e) {
e.printstacktrace();
}
}
/**
*<br>方法说明:主方法
*<br>输入参数:
*<br>返回类型:
*/
public static void main(string[] args) {
jdomoption jpt = new jdomoption();
}
}
闽公网安备 35060202000074号