码迷,mamicode.com
首页 > 其他好文 > 详细

2014年10月13日写:修改添加xml元素

时间:2014-10-13 16:26:49      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   os   ar   java   sp   div   

package haha;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class ModiXmlFile {
    private String filename;
    private SAXReader saxReader;
    private Document document;

    ModiXmlFile(String _filename) {
    this.filename = _filename;
    saxReader = new SAXReader();
    try {
        document = saxReader.read(filename);
    } catch (DocumentException e) {
        System.err.println("获取 [" + filename
            + "]文档时,发生了错误");
    }
    }
    /**
     * 在所有的二级元素下添加元素,若已有则不填加。
     * @param nodeName 元素名
     * @param initValue 初始值
     * @return 若添加了元素,返回true。没有添加返回false
     */
    @SuppressWarnings("unchecked")
    public Boolean addNode(String nodeName, String initValue) {
    Element root = document.getRootElement();
    List<Element> elements = root.elements();
    Iterator<Element> it = elements.iterator();
    Element tempElement;
    Boolean result=false;
    while (it.hasNext()) {
        tempElement=it.next();
        if (tempElement.element(nodeName)==null) {
        tempElement.addElement(nodeName).setText(initValue);
        result=true;
        }
    }
    if (result) {
        if (fflush())
            return true;
        else
            return false;
    }
    else {
        return false;
    }
    }
/**
 * 根据所给的id,修改第id个二级元素拥有的指定元素的值
 * @param id 二级元素的次序
 * @param nodeName 指定元素的名字
 * @param value 值
 * @return 修改成功或失败(没有此元素);
 */
    @SuppressWarnings("unchecked")
    public Boolean setNodeValue(Integer id, String nodeName, String value) {
    Element root = document.getRootElement();
    List<Element> elements = root.elements();
    Iterator<Element> it = elements.iterator();
    boolean result=false;
    Element tempElement;
    while (it.hasNext()) {
        tempElement=it.next();
        if (--id!=0) {
        continue;
        }
        if (tempElement==null) {
        System.err.println("没有此节点");
        return false;
        }
        tempElement.element(nodeName).setText(value);
        result=true;
        break;
    }
    if (!result) {
        System.err.println("指定的次序超过了总二级元素个数");
        return false;
    }
    else if (fflush()) {
        return true;
    }
    else {
        return false;
    }
    }
    private Boolean fflush() {
    XMLWriter output;
    try {
        output = new XMLWriter(new FileWriter(new File(filename)));
        output.write(document);
        output.close();
    } catch (IOException e) {
        System.err.println("打开 [" + filename + "],发生了错误");
        return false;
    }
    return true;
    }
}

 

2014年10月13日写:修改添加xml元素

标签:style   blog   color   io   os   ar   java   sp   div   

原文地址:http://www.cnblogs.com/imyijie/p/4022045.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!