当前位置:编程学习 > JSP >>

java 的jdom使用类

答案: 

/*************************************************************
* 功能完成对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]); /

上一个:Java如何调用可执行文件和批处理命令
下一个:Java异常处理的陋习展播

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,