Written in December October 13, 2014: Modify and add XML elements

Source: Internet
Author: User

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;    }}

 

Written in December October 13, 2014: Modify and add XML elements

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.