惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

F
Fortinet All Blogs
Attack and Defense Labs
Attack and Defense Labs
V2EX - 技术
V2EX - 技术
O
OpenAI News
S
Secure Thoughts
H
Heimdal Security Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Schneier on Security
Schneier on Security
H
Hacker News: Front Page
S
Security Affairs
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
The Register - Security
The Register - Security
GbyAI
GbyAI
Cloudbric
Cloudbric
MongoDB | Blog
MongoDB | Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
K
Kaspersky official blog
Forbes - Security
Forbes - Security
Y
Y Combinator Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
Scott Helme
Scott Helme
Hacker News - Newest:
Hacker News - Newest: "LLM"
The Cloudflare Blog
Recorded Future
Recorded Future
人人都是产品经理
人人都是产品经理
Cyberwarzone
Cyberwarzone
C
CERT Recently Published Vulnerability Notes
Webroot Blog
Webroot Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
L
LangChain Blog
T
Tor Project blog
Microsoft Azure Blog
Microsoft Azure Blog
博客园_首页
Hacker News: Ask HN
Hacker News: Ask HN
Blog — PlanetScale
Blog — PlanetScale
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
B
Blog RSS Feed
N
News and Events Feed by Topic
阮一峰的网络日志
阮一峰的网络日志
I
Intezer
V
V2EX
T
Tailwind CSS Blog
SecWiki News
SecWiki News
NISL@THU
NISL@THU
C
Check Point Blog

博客园 - RedFox(低调)

多大的船决定要承担多大的阻力,多大的风浪会炼出多厉害的船长,大副,二副,水手。。。。很多时候不是你选择了风浪,而是风浪选择并决定了你!优秀的水手觉得不是战胜了风浪,而是适应了风浪。 jdk国内镜像 基于vue-cli的vs code设置 maven 国内完整源 怎么看待MYSQL的性能 cas4.2的安装 java websocket 解决openresty http客户端不支持https的问题 开源许可证GPL、BSD、MIT、Mozilla、Apache和LGPL的区别 为何Google这类巨头会认为敏捷开发原则是废话? git ldap 获取大众点评数据 svn for vs SQL 导出表数据存储过程 色彩设计,是产品设计,营销渠道的一个重要环节 sql xml 腾讯或正在陷入全面危机? 项目实施与管理的几点建议 ERP与进销存软件的区别
java dom4j 读写XML
RedFox(低调) · 2016-11-29 · via 博客园 - RedFox(低调)
<?xml version="1.0" encoding="UTF-8"?>

<Configuration> 
  <Config id="sysId">666</Config>  
  <Config id="sysName">en</Config> 
</Configuration>
import com.google.common.base.MoreObjects;
import org.dom4j.*;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;


import java.io.*;
import java.util.List;

/**
 * Created by wfcfan on 2016/11/29.
 */
public class UserConfig implements Serializable {

    static private String CONFIG_XML_PATH = "D:\\data\\config.xml";


    static private synchronized Document getConfigDocument() {
        SAXReader reader = new SAXReader();
        Document document = null;
        try {
            if (CONFIG_XML_PATH.startsWith("/")) {
                document = reader.read(new File(CONFIG_XML_PATH));
            } else {
                document = reader.read(CONFIG_XML_PATH);
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return document;
    }

    static public synchronized UserConfig getUserConfig() throws DocumentException {

        Element root = getConfigDocument().getRootElement();

        List<Element> eles = root.elements();
        UserConfig config = new UserConfig();
        for (Element ele : eles) {
            String idAttr = ele.attribute("id").getValue();
            if (idAttr.equals("sysId")) {
                config.setSysId(Integer.parseInt(ele.getTextTrim()));
            } else if (idAttr.equals("sysName")) {
                config.setSysName(ele.getTextTrim());
            }
        }

        return config;
    }

    static public synchronized void setUserConfig(UserConfig config) {
        Document doc = getConfigDocument();
        Element root = doc.getRootElement();

        List<Element> eles = root.elements();
        for (Element ele : eles) {
            String idAttr = ele.attribute("id").getValue();
            if (idAttr.equals("sysId")) {
                ele.setText(String.valueOf(config.getSysId()));
            } else if (idAttr.equals("sysName")) {
                ele.setText(config.getSysName());
            }
        }

        OutputFormat format = OutputFormat.createPrettyPrint();
        XMLWriter writer = null;

        try {
            writer = new XMLWriter(new FileOutputStream(CONFIG_XML_PATH), format);
            writer.write(doc);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    public UserConfig() {

    }

    public UserConfig(int id, String sysName) {
        this.sysId = id;
        this.sysName = sysName;
    }

    private int sysId;
    private String sysName;


    public int getSysId() {
        return sysId;
    }

    public void setSysId(int sysId) {
        this.sysId = sysId;
    }

    public String getSysName() {
        return sysName;
    }

    public void setSysName(String sysName) {
        this.sysName = sysName;
    }


    @Override
    public String toString() {
        return MoreObjects.toStringHelper(this)
                .add("sysId", sysId)
                .add("sysName", sysName)
                .toString();
    }
}

posted on 2016-11-29 14:29  RedFox(低调)  阅读(305)  评论()    收藏  举报