java读取xml配置文件

xml文件如下 配置功能开关

<Configurations>
    <Samples>true</Samples>
    <Excepts>true</Excepts>
    <CheckFace>false</CheckFace>
    <ThumbNail>false</ThumbNail>
    <ScanDir>xuzhen</ScanDir>
     <SampleDir>G</SampleDir>
    <ExceptDir>G:</ExceptDir>
    <PicScanDB>G:</PicScanDB>
    <DBName>xuzhen</DBName>
    <DBHost>sdfsdfsdf</DBHost>
    <DBUser>xuzhen</DBUser>
    <EthIP>192.168.0.117</EthIP>
    <DBPassWd>xuzhen</DBPassWd>
    <Spec>*.jpg;*.jpeg;*.jpe;*.jfif;*.bmp;*.dib</Spec>
    <After>1980-01-02 00:00:00</After>
    <InitThreadNum>20</InitThreadNum>
    <MaxThreadNum>40</MaxThreadNum>
    <LogLevel>9999</LogLevel>
    <DeleteChecked>FALSE</DeleteChecked>
</Configurations>

 

操作类

package com.ibstech.controller.syntax;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.InvalidPropertiesFormatException;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Entity;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.opensymphony.xwork2.ActionSupport;
import com.sun.org.apache.xml.internal.serialize.OutputFormat;
import com.sun.org.apache.xml.internal.serialize.XMLSerializer;

public class ServerManager {
    public static HashMap GetXML() {

        HashMap hm = new HashMap();
        try {
            String xmlFile = "C://Documents and Settings//Administrator//workspace//MyTest//WebContent//config.xml";
            File file = new File(xmlFile);
            if (file.exists()) {
                DocumentBuilderFactory factory = DocumentBuilderFactory
                        .newInstance();
                DocumentBuilder builder = factory.newDocumentBuilder();
                Document doc = builder.parse(file);
                NodeList nodeList = doc.getChildNodes();
                Node nod = nodeList.item(0);
                nodeList = nod.getChildNodes();

                for (int i = 0; i < nodeList.getLength(); i++) {
                    nod = nodeList.item(i);
                    // 是文本
                    if (nod.getNodeType() != 3) {
                        hm.put(nod.getNodeName(), nod.getTextContent());
                        System.out.println("键" + nod.getNodeName() + "值"
                                + nod.getTextContent());
                    }
                }
            }
        } catch (Exception ex) {
        }
        return hm;
    }

    public static void SetXML(HashMap map) {

        //map用来存放最新设置的参数在下面将map得到的与以前相比较
        //如果xml中节点的名字与这个相同则就更新他的context内容
        try {
            String xmlFile = "C://Documents and Settings//Administrator//workspace//MyTest//WebContent//config.xml";
            File file = new File(xmlFile);
            if (file.exists()) {
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                DocumentBuilder builder = factory.newDocumentBuilder();
                Document doc = builder.parse(file);
                NodeList nodeList = doc.getChildNodes();
                Node nod = nodeList.item(0);
                nodeList = nod.getChildNodes();
                for (int i = 0; i < nodeList.getLength(); i++) {
                    nod = nodeList.item(i);

                    if (nod.getNodeType() != 3) {
                       
                       
                        //遍历我得到节点与xml中通过tagname比较如果有的就把他的context改为我现在设置的
                        //迭代器用来遍历map
                        Iterator it=map.entrySet().iterator();
                        while(it.hasNext())
                        {
                           
                        Map.Entry ent=(Map.Entry)it.next();                   
                        if (nod.getNodeName() ==ent.getKey()) {
                                nod.setTextContent((String) ent.getValue());
                                //向xml中写入
                                OutputFormat format = new OutputFormat(doc);
                                format.setOmitXMLDeclaration(true); 
                                XMLSerializer   output   =   new XMLSerializer(new java.io.FileWriter(xmlFile,   false),   format); 
                                output.serialize(doc);  
                               
                            }
                        }
                       
                    }
                   

                }
               
            }
        } catch (Exception ex) {
       
        }

    }

    public static void main(String[] args) {
        //ServerManager.SetXML();
        System.out.println(ServerManager.GetXML());
        File file=new File("");
        System.out.println(file.getAbsolutePath());
    }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值