java中jdom,java 的jdom使用类-JSP教程,Java技巧及代码

/*************************************************************

* 功能完成对xml文件的 建立,增,删,改,显示,查找(值比对)

* 现在只支持二级标签生成

*

*  

*     值

*     值

*  

*

* main()         为使用小样

* **************************************************************

* @author        sanshi

* create         2005/06/09

* 感谢:          张山,梅春,花开石头(任何帮助:包括精神虐待和肉体蹂躏)

* ***************************************************************

* @param filename 文件名

* @param create   文件不存在是否建立,默认为false(不建立)

* @param root     建立文件使用,代表根元素

* @param child    建立文件使用,代表一级标签

* @param twochild 建立文件使用,代表二级标签。样式a[0][0]=”标签名”,a[0][1]=”标签值”

* @param debug    是否打印调试信息,对整个类有作用,默认为false不打印

*/

import org.jdom.output.xmloutputter;

import org.jdom.document;

import org.jdom.element;

import java.util.list;

import org.jdom.output.format;

import org.jdom.input.saxbuilder;

import java.io.file;

import java.io.fileoutputstream;

import java.io.ioexception;

import java.io.fileinputstream;

public class xmlwork {

private string filename; //文件名

private boolean create = false; //文件不存在是否建立

private string root; //xml的根

private string child; //一级标签

private string[][] twochild; //二级标签

private boolean debug = true; //是否打印调试信息

/**********************调试开始***********************************/

public static void main(string args[]) {

string[][] a = new string[2][2];

a[0][0] = “name”;

a[0][1] = “sanshi”;

a[1][0] = “count”;

a[1][1] = “1”;

xmlwork t = new xmlwork();

t.xmlwork(“zl.xml”, 2, true); //第一步设置参数

t.settga(“root”, “child”, a); //建立文件设置标签

//t.creatxmlfile();

string[] value = new string[2];

value[0] = “sanshi”;

value[1] = “3”;

t.showallelement(1,0);//

if(t.isfindelement(value))

{

system.out.println(“找到了!”);

}else{

system.out.println(“没找到!”);

}

//t.editxmlelement(0,value);

//t.addxmlelement(value);

//t.delectxmlelement(0);

}

/*************************调试结束*******************************/

/**

* 设置是否打印调试信息

* @param boolean debug false/不打印 ture/打印

*/

public void setdebug(boolean debug) {

this.debug = debug;

}

/**

* 取得当前的调试信息

* @return boolean

*/

public boolean getdebug() {

return debug;

}

//判断文件名和一些常用变量是否存在

public boolean istrue() {

boolean check = true;

if (filename == null || root == null || child == null ||

twochild.length <= 0) {

check = false;

}

return check;

}

//初始化

public xmlwork() {}

/**

* 判断输入文件名的合法性

* @param string  filename  文件名字/打开或者建立

* @param int     list      二级标签的大小

* @param booleam crrate    如果不存在是否建立新文件

*/

public void xmlwork(string filename, int list, boolean create) {

if (filename == null || filename.length() <= 0) {

system.out.println(“你没有输入文件名”);

system.exit(0);

}

else if (filename.indexof(“.xml”) <= 0) {

system.out.println(“你输入文件名不正确”);

system.exit(0);

}

else {

this.filename = filename;

this.create = create;

string[][] children = new string[list][list];

this.twochild = children;

//调试信息输出

if (debug) {

system.out.println(“文件名设置成功=====>” + filename);

system.out.println(“二级标签共有=======>” + list + “个”);

}

}

}

//标签设置

public void settga(string root, string child, string[][] twochild) {

if (root == null || child == null) {

system.out.println(“你没有输入根标签或者是一级标签”);

system.exit(0);

}

else if (twochild.length <= 0) {

system.out.println(“你没有输入二级标签”);

system.exit(0);

}

else {

this.root = root;

this.child = child;

this.twochild = twochild;

if (debug) { //调试信息输出

system.out.println(“标签设置成功!”);

system.out.println(“根标签为==>” + root);

system.out.println(“一级标签为==>” + child);

system.out.println(twochild[0][0] + “====>” + twochild[0][1]);

system.out.println(twochild.length);

}

}

}

//判断您传递近来的文件是否为有效

private boolean isfile() {

file file = new file(filename);

return file.exists();

}

public void printdebug() {

if (!istrue()) {

system.out.println(“缺少参数”);

system.exit(0);

}

if (isfile()) {

//system.out.println(“文件文法打开”);

//system.exit(0);

}

}

public void creatxmlfile() {

printdebug();

//这以下根据需求更改——开始

element newroot = new element(root); //设置根标签

element newchild = new element(child); //设置单元标签//根下一级

for (int i = 0; i < twochild.length; i++) {

element count = new element(twochild[i][0]); //根下二级

count.settext(twochild[i][1]); //设置二级标签的值值

//count.setattribute( “属性名”, “属性值” );//添加一个属性

newchild.addcontent(count); //把二级添加到一级

}

newroot.addcontent(newchild); //把一级添加到根

document doc = new document(newroot); //把根写到document

//设置生成wml

//doc.setdoctype(new doctype(“wml”,”-//wapforum//dtd wml 1.1//en”,”http://www.wapforum.com/dtd/wml_1.1.xml“));

//根据需求更改结束

xmloutputter xmlout = new xmloutputter(); //生成xml的输出流

format format = xmlout.getformat(); //把输出流格式化

format.setencoding(“gbk”); //设置字符集

format.setexpandemptyelements(true); //是否填充

xmlout.setformat(format); //把格式化的流给输出流

try {

//生成xml的文件,文件名为用户输入的文件

xmlout.output(doc, new fileoutputstream(filename));

system.out.println(“文件建立成功文件名===>” + filename);

}

catch (ioexception ex) {

system.out.println(“file create failing” + ex.getmessage());

}

}

/**

* 此方法只是增加固定标签的值,在最后添加,如果文件不存在,而同意建立的话话建立

* @param string[] value 添加值的数组,按二级标签顺序存放

*/

public void addxmlelement(string[] value) {

printdebug();

if (!this.isfile() && create) { //打开失败建立

creatxmlfile();

}

//开始添加

fileinputstream fi = null; //文件输入流

fileoutputstream fo = null; //文件输出流

file file = new file(filename);

try {

fi = new fileinputstream(file); //建立打开流

saxbuilder sb = new saxbuilder();

document doc = sb.build(fi); //把文件流给build

element root = doc.getrootelement(); //得到根元素

list childsum = root.getchildren(); //得到根元素的集合

element newchild = new element(child);

for (int i = 0; i < twochild.length; i++) {

//生成新元素

element newcount = new element(twochild[i][0]);

newcount.settext(value[i]);

newchild.addcontent(newcount); //在新根下添加新元素

}

childsum.add(newchild); //在xml文件里添加新的东西

xmloutputter xmlout = new xmloutputter();

format format = xmlout.getformat();

format.setencoding(“gbk”); //设置字符集

format.setexpandemptyelements(true); //打开添加

format.setindent(” “); //设置分割符

//format.setomitdeclaration(true);//是否添加头信息

xmlout.setformat(format);

fo = new fileoutputstream(file);

xmlout.output(doc, fo);

if (debug) {

system.out.println(“添加成功”);

}

}

catch (exception e) {

system.out.println(“文件打开失败,文件名为:” + filename + “<==>错误原因为:” +

e.getmessage());

}

finally {

try {

fi.close();

fo.close();

}

catch (exception e) {

e.printstacktrace();

}

}

}

/**

* 此方法删除指定的值,也就是xml中的第几个孩子

* @param elementid  为孩子id。xml默认从0开始

*/

public void delectxmlelement(int elementid) {

//printdebug();

fileinputstream fi = null; //文件输入流

fileoutputstream fo = null; //文件输出流

file file = new file(filename);

try {

if (!this.isfile()) { //文件打开失败

system.out.println(“文件打开失败,文件名为:” + filename);

}

else {

fi = new fileinputstream(file); //建立打开流

saxbuilder sb = new saxbuilder();

document doc = sb.build(fi); //把文件流给build

element root = doc.getrootelement(); //得到根元素

list childsum = root.getchildren(); //得到根元素的集合

if (elementid >= childsum.size()) {

system.out.println(“超过长度!”);

system.exit(0);

}

childsum.remove(elementid);

xmloutputter xmlout = new xmloutputter();

format format = xmlout.getformat();

format.setencoding(“gbk”); //设置字符集

format.setexpandemptyelements(true); //打开添加

format.setindent(” “); //设置分割符

//format.setomitdeclaration(true);//是否添加头信息

xmlout.setformat(format);

fo = new fileoutputstream(file);

xmlout.output(doc, fo);

if (debug) {

system.out.println(“删除成功”);

}

}

}

catch (exception e) {

system.out.println(“文件打开失败,文件名为:” + filename + “<==>错误原因为:” +

e.getmessage());

}

finally {

try {

fi.close();

fo.close();

}

catch (exception e) {

e.printstacktrace();

}

}

}

/**

* 此方法编辑指定的值,也就是xml中的第几个孩子

* @param elementid  为孩子id。xml默认从0开始

* @param value      数组为修改为的值

*/

public void editxmlelement(int elementid, string[] value) {

fileinputstream fi = null; //文件输入流

fileoutputstream fo = null; //文件输出流

try {

if (!this.isfile()) { //文件打开失败

system.out.println(“文件打开失败,文件名为:” + filename);

}

else {

fi=new fileinputstream(filename);//建立打开流

saxbuilder sb=new saxbuilder();

document doc=sb.build(fi);//把文件流给build

element root=doc.getrootelement();//得到根元素

list childsum=root.getchildren();//得到根元素的集合

if (elementid >= childsum.size()) {

system.out.println(“超过长度!”);

system.exit(0);

}

element edit=(element)childsum.get(elementid);//选择编辑元素

for(int i = 0; i < twochild.length; i++){

element newname=edit.getchild(twochild[i][0]);//生成新的标签

newname.settext(value[i]);//设置新值

}

childsum.set(elementid,edit);//修改

xmloutputter xmlout=new xmloutputter();

format format=xmlout.getformat();

//format.setencoding(“gbk”);

format.setexpandemptyelements(true);

format.setindent(” “);

//format.setomitdeclaration(true);

xmlout.setformat(format);

fo=new fileoutputstream(filename);

xmlout.output(doc,fo);

if (debug) {

system.out.println(“编辑成功”);

}

}

}

catch (exception e) {

system.out.println(“文件打开失败,文件名为:” + filename + “<==>错误原因为:” +

e.getmessage());

}

finally {

try {

fi.close();

fo.close();

}

catch (exception e) {

e.printstacktrace();

}

}

}

/**

*@param int start     传入开始输出的项

*@param int end       结束输出的项,为0表示最后一个。

*/

public void showallelement(int start,int end)

{

fileinputstream fi = null; //文件输入流

//fileoutputstream fo = null; //文件输出流

try{

if (!this.isfile()) { //文件打开失败

system.out.println(“文件打开失败,文件名为:” + filename);

}

fi=new fileinputstream(new file(filename));//建立打开流

saxbuilder sb=new saxbuilder();

document doc=sb.build(fi);//把文件流给build

element root=doc.getrootelement();//得到根元素

list childsum=root.getchildren();//得到根元素的集合

if(childsum.size()==0)

{

system.out.println(“该文件没有内容!文件名为===>”+filename);

system.exit(1);

}

if(end==0)

{

end=childsum.size();

}

if(start<0||end>childsum.size()||start==end||start>end)

{

system.out.println(“输入参数有错误==>start=”+start+”||==>end=”+end);

system.exit(0);

}

for(int i=start;i

{

element ch=(element)childsum.get(i);

for(int j=0;j

{

string p = ch.getchild(twochild[j][0]).gettext();

system.out.println(p);

}

}

if(debug)

{

system.out.println(“输出完成”);

}

}catch (exception e) {

system.out.println(“文件打开失败,文件名为:” + filename + “<==>错误原因为:” +

e.getmessage());

}

finally {

try {

fi.close();

//fo.close();

}

catch (exception e) {

e.printstacktrace();

}

}

}

/**

*@param string[]  value   为比对的值对

* @return boolean         是否查找成功

* */

public boolean isfindelement(string[] value)

{

fileinputstream fi = null; //文件输入流

//fileoutputstream fo = null; //文件输出流

boolean count=true;

try{

if (!this.isfile()) { //文件打开失败

system.out.println(“文件打开失败,文件名为:” + filename);

}

fi=new fileinputstream(new file(filename));//建立打开流

saxbuilder sb=new saxbuilder();

document doc=sb.build(fi);//把文件流给build

element root=doc.getrootelement();//得到根元素

list childsum=root.getchildren();//得到根元素的集合

if(childsum.size()==0)

{

system.out.println(“该文件没有内容!文件名为===>”+filename);

system.exit(1);

}

for(int i=0;i

{

count=true;

element ch=(element)childsum.get(i);

for(int j=0;j

{

string p = ch.getchild(twochild[j][0]).gettext();

if(!p.equals(value[j]))

{

count=false;

}

}

if(count)

{

return count;

}

}

if(debug)

{

system.out.println(“查找完成”);

}

}

catch (exception e) {

system.out.println(“文件打开失败,文件名为:” + filename + “<==>错误原因为:” +

e.getmessage());

}

finally {

try {

fi.close();

//fo.close();

}

catch (exception e) {

e.printstacktrace();

}

}

return count;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值