关于XML解析的一些代码

最近项目中需要添加一个给客户发送消息的功能,需要把消息模板放在xml文件中,模板如下:

<?xml version="1.0"?>
<root>
  <name>张三</name>
  <age>23</age>
  <notice>尊敬的 {0} 用户,您好:平台撤销了 {1} 产品下的 {2}、{3} 服务,请您根据情况进行修改!</notice>
</root>

需要在相对应的位置上输入用户名、产品名、服务名等信息,写了一个解析xml的工具类,如下:

package bean;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;

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

public class XMLUtil
{
    private final static String PATH = "C:\\Users\\DOG\\Desktop\\XML.xml";
    private final static Map<String, String> MAP = new HashMap<String, String>();   
    private volatile static XMLUtil util = null;    
    private XMLUtil()
    {
        init();
    }

    public static XMLUtil getInstance()
    {
        if(util == null)
        {
            synchronized(XMLUtil.class)
            {
                if(util == null)
                {
                    util = new XMLUtil();
                }
            }
        }       
        return util;
    }


    private void init()
    {
        try
        {
            Document document = new SAXReader().read(new FileInputStream(new File(PATH)));
            Element rootElement = document.getRootElement();
            for(Object obj:rootElement.elements())
            {
                Element element = (Element)obj;
                MAP.put(element.getName().trim(), element.getText().trim());
            }
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (DocumentException e)
        {
            e.printStackTrace();
        }       
    }
    public String getValue(String key)
    {
        if("".equals(key) || key == null)
        {
            return "key值为空!";
        }
        else
        {
            if(MAP.get(key) == null)
            {
                return key+"对应的值为空!";
            }
        }
        return MAP.get(key);
    }
    public String getValue(String key,Object...arg)
    {
        if("".equals(key) || key == null)
        {
            return "key值为空!";
        }
        else
        {
            if(getValue(key) == null)
            {
                return key+"对应的值为空!";
            }
        }
        //重新定义一个format方法
        return format(getValue(key), arg);
//      jdk封装自带有往{}里传值的方法,MessageFormat.format(String str,Object[] obj)
//      return MessageFormat.format(getValue(key), arg);
    }   
    private String format(String key,Object...obj)
    {
        char[] ch = key.toCharArray();
        int count = 0;
        for(int i=0;i<ch.length;i++)
        {
            if('{' == ch[i])
            {
                count++;
            }
        }
        String a = key;
        for(int i=0;i<count;i++)
        {
            a = a.replace("{"+i+"}", (String)obj[i]);
        }
        return a;
    }
}

只需把不定参数传入,即可调用该方法,测试一下:

package bean;

public class Test
{
    public static void main(String[] args)
    {
        String message = XMLUtil.getInstance().getValue("notice", "张三","微信","发红包","收红包");
        System.out.println(message);
    }
}

输出结果如下:

尊敬的 张三 用户,您好:平台撤销了 微信 产品下的 发红包、收红包 服务,请您根据情况进行修改!
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值