`

读取xml,返回对象

    博客分类:
  • java
 
阅读更多

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.howbuy.core.util.StringUtil;
import com.howbuy.trade.dto.CorpMerchantDto;

/**
 * @ClassName: CorpMerchantUtil
 * @Description: 合作商户信息操作工具类
 * @date 2013-4-27 上午10:46:48
 *
 */
public class CorpMerchantUtil {
    /**
     * logger
     */
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
   
    /**
     * instance
     */
    private static CorpMerchantUtil instance;
    private final static String fileName = "corpMerchant.xml";
    private static Map<String, CorpMerchantDto> corpMerchantsMap;
    private static String currentVersion = "";
   
    private CorpMerchantUtil() {
        loadCorpMerchants();
    }
   
    /**
     * getInstance
     * @return CorpMerchantUtil
     */
    public static CorpMerchantUtil getInstance() {
        if(null == instance || isUpdateCorpMerchants()) {
            instance = new CorpMerchantUtil();
        }
        return instance;
    }
   
    /**
     * getCorpMerchantsMap
     * @return Map<String, CorpMerchantDto>
     */
    public Map<String, CorpMerchantDto> getCorpMerchantsMap() {
        return corpMerchantsMap;
    }
   
    /**
     * 是否存在指定商户代码
     * @param corpID 商户代码
     * @return boolean true:是,false:否
     */
    public boolean isExistsCorpMerchantByCorpID(String corpID) {
        if(StringUtil.isEmpty(corpID)) {
            return false;
        }
        return corpMerchantsMap.containsKey(corpID);
    }
   
    /**
     * 根据商户代码获取商户信息
     * @param corpID 商户代码
     * @return CorpMerchantDto 商户信息
     */
    public CorpMerchantDto getCorpMerchantByCorpID(String corpID) {
        if(StringUtil.isEmpty(corpID) || !corpMerchantsMap.containsKey(corpID)) {
            return null;
        }
        return corpMerchantsMap.get(corpID);
    }
   
    /**
     * 是否需要更新商户列表
     * @return boolean
     */
    private static boolean isUpdateCorpMerchants() {
        boolean isUpdate = false;
       
        SAXReader saxReader = new SAXReader();
        Document document;
        try {
            document = saxReader.read(Thread.currentThread().getContextClassLoader().getResource(fileName));

            Element root = document.getRootElement();
            Attribute attribute = root.attribute("version");
            if(null != attribute) {
                String version = attribute.getValue();
                if(!currentVersion.equals(version)) {
                    isUpdate = true;
                }
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
       
        return isUpdate;
    }
   
    /**
     * 加载查询条件内容
     */
    private void loadCorpMerchants() {
        logger.info("******************** Load cooperation merchants*********************");
        corpMerchantsMap = new HashMap<String, CorpMerchantDto>();
       
        SAXReader saxReader = new SAXReader();
        Document document;
        try {
            document = saxReader.read(Thread.currentThread().getContextClassLoader().getResource(fileName));

            Element root = document.getRootElement();
            Attribute attribute = root.attribute("version");
            String version = attribute.getValue();
            currentVersion = version;
            for(Iterator iter = root.elementIterator(); iter.hasNext();) {
                Element element = (Element)iter.next();
                CorpMerchantDto corpMerchantDto = new CorpMerchantDto();
                Attribute attributeOfCorpID = element.attribute("corpID");
                Attribute attributeOfCoopID = element.attribute("coopID");
                Attribute attributeOfActID = element.attribute("actID");
                corpMerchantDto.setCorpID(attributeOfCorpID.getValue());
                corpMerchantDto.setCoopID(attributeOfCoopID.getValue());
                corpMerchantDto.setActID(attributeOfActID.getValue());
                corpMerchantDto.setMerchantName(element.getText());
               
                corpMerchantsMap.put(attributeOfCorpID.getValue(), corpMerchantDto);
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }
   
    /**
     * test
     * @param args
     * void
     */
    public static void main(String[] args) {
        Map<String, CorpMerchantDto> map = CorpMerchantUtil.getInstance().getCorpMerchantsMap();
        for(Entry<String, CorpMerchantDto> entry : map.entrySet()) {
            System.out.println("corpID = " + entry.getValue().getCorpID());
            System.out.println("merchantName = " + entry.getValue().getMerchantName());
        }
    }
}

xml

<?xml version="1.0" encoding="UTF-8"?>
<merchants version="1.0">
    <merchant corpID="000001" coopID="W20130701" actID="HD0001">盈盈理财</merchant>
</merchants>

 

public class CorpMerchantDto implements Serializable {
    /**
     * @Fields serialVersionUID: 2005927276049639976L
     */
    private static final long serialVersionUID = 2005927276049639976L;
   
    /**
     * 商户代码
     */
    private String corpID;
   
    /**
     * 商户名称
     */
    private String merchantName;
   
    /**
     * 合作代码
     */
    private String coopID;
   
    /**
     * 活动代码
     */
    private String actID;

    /**
     * @return the corpID
     */
    public String getCorpID() {
        return corpID;
    }

    /**
     * @param corpID the corpID to set
     */
    public void setCorpID(String corpID) {
        this.corpID = corpID;
    }

    /**
     * @return the merchantName
     */
    public String getMerchantName() {
        return merchantName;
    }

    /**
     * @param merchantName the merchantName to set
     */
    public void setMerchantName(String merchantName) {
        this.merchantName = merchantName;
    }

    /**
     * @return the coopID
     */
    public String getCoopID() {
        return coopID;
    }

    /**
     * @param coopID the coopID to set
     */
    public void setCoopID(String coopID) {
        this.coopID = coopID;
    }

    /**
     * @return the actID
     */
    public String getActID() {
        return actID;
    }

    /**
     * @param actID the actID to set
     */
    public void setActID(String actID) {
        this.actID = actID;
    }

 

分享到:
评论

相关推荐

    使用JS读取XML文件的方法

    由于项目上需要解析xml,于是各种百度,...第二类:firefox,opera:用构造函数 DOMParser()实例化DOMParser对象,解析xml文本,并返回xml Document对象; 第三类:chrome,safari:由于chrome不支持load方法,故用http

    ios_XML_解析

    他说他是直接返回一串字符串的,是底层给疯装成xml返回的 嗨,累 网上搜了下,有说用sdk自带的NSXMLParse, 有说用google提供的GDataXML, 还有说用TBXML、KissXML等等的 可是NSXMLParse要实现代理,多出一大堆...

    基于jdk1.6的傻瓜xml解析

    只要针对xml文件定义一个javaBean类,传入类的class对象,就可以通过本工具自动返回带有数据的bean类的实例。定义类的类名必要是标签名,类的属性对应标签的属性。本工具可以实现嵌套标签的解析,对应的bean定义规则...

    Tinyxml 源代码(VC6 & VS2005)

    从流中读取XML使其可用于网络传输。通过些小技巧,它知道当XML文档读取完毕时,流后面的就一定是其它数据了。TinyXML总假定当它读取到根结点后XML数据就结束了。换句话说,那些具有不止一个根元素的文档是无法被正确...

    FlashMXAction简易手册.zip

    读取该属性时,返回 myXML 操作对象目前使用的节点名称。注意:只有 XML 元素 (nodeType == 1) 节点可以拥有节点名称,如果访问一个 XML 文本 (nodeType == 3) 节点的 nodeName 属性,将返回 null 。

    xml入门教程/xml入门教程

    &1.XML简介 XML的背景 1) XML代表可扩展的标记语言(eXtensible Markup Language); 2) XML由W3C联盟发展维护; 3) XML是一种元语言,可以用来定义其它标签语言; 4) XML没有定义任何标记,它提供了一种工具定义...

    DomReadXmlUtils.java

    xml文件解析,读取xml文件,需要new 这个工具类,然后给类一个泛型对应xml里的元素就行,方法传入3个参数xml文件地址节点名和对应实体类的类型,可以读取xml文件返回对象集合。他不同于sax解析,他是把文件加载到...

    sdmx.py:使用 Python 读取 SDMX XML 文件

    安装pip install sdmx 用法sdmx.generic_data_message_reader(fileobj, dsd_fileobj=None, lazy=None) 给定一个表示通用数据消息的 XML 的类文件对象,返回一个数据消息阅读器。sdmx.compact_data_message_reader...

    XML轻松学习手册--XML肯定是未来的发展趋势,不论是网页设计师还是网络程序员,都应该及时学习和了解

    在XML中,就是要将网页也作为一个对象来操作和控制,我们可以建立自己的对象和模板。与对象进行交流,如何命令对象,就要用到API。API全称Application Programming Interface,它是访问和操作对象的规则。而DOM就是...

    动态树形结构菜单,从数据库中动态读取节点,通过XML和JS创建树形结构

    从数据岛menuXML中读取数据,从树的根节点开始分析树, 利用 hasChildNodes() [方法:是否含有子节点 ] 判断当前 节点是否有子节点,如果有子节点继续向下分析 childNodes [对象:子节点对象集合] ,否则返回当前分析结果...

    SaxReadXmlUtils.java

    xml文件解析,读取xml文件,需要new 这个工具类,然后给类一个泛型对应xml里的元素就行,方法传入3个参数xml文件地址节点名和对应实体类的类型,可以读取xml文件返回对象集合。

    javascript读取xml

    代码如下:/** * 得到XML文件属性的集合对象 * @param xmlDoc XML对象 * @param name 属性名称 如: user * @return 返回 Array 对象 * Example XML: * &lt;?xml version=’1.0′ encoding=’...

    python 解析XML python模块xml.dom解析xml实例代码

    一 、python模块 xml.dom 解析XML的APIminidom.parse(filename)加载读取XML文件 doc.documentElement获取XML文档对象 node.getAttribute(AttributeName)获取XML节点属性值 node.getElementsByTagName(TagName)获取...

    springmybatis

    查询出列表,也就是返回list, 在我们这个例子中也就是 List&lt;User&gt; , 这种方式返回数据,需要在User.xml 里面配置返回的类型 resultMap, 注意不是 resultType, 而这个resultMap 所对应的应该是我们自己配置的 ...

    XML纯解析源码(JAVA 不含DOM类)

    * @return 返回标签对象 */ public HtmlFlag parseFlag(HtmlFlag Super, String f) {} /** * 读取指定标签属性值,(以#为前缀则同过id属性值索引) * HTML.body.table:align :表示取table的align属性值...

    XML操作动态库

    没有错误1:初始化对象失败2:没有加载文件3:找不到指定文件4:XML文档错误 &lt;br&gt;//5:节点在存在6:属性不存在7:添加节点失败8:添加属性失败9:保存文档失败 &lt;br&gt;//10:删除节点失败11:读取参娄失败12:修改结点失败...

    DWR.xml配置文件说明书(含源码)

    scripted script 返回远程对象的脚本,脚本可以指定一些属性,多数情况下一般只设置param节点配置.属性很少设置. spring Location* 任何以location开头的参数,每个参数都是指定一个spring的配置文件,在参数没有设置的...

    Android利用Dom对XML进行增删改查操作详解

    但是,其他有些服务器会返回XML格式的文件,这时候就需要去读取XML文件了。 XML的解析有三种方式,在Android中提供了三种解析XML的方式:DOM(Document Objrect Model) , SAX(Simple API XML) ,以及Android推荐的Pull...

    AJAX开发简略含续一(PDF 包含源代码)

    目录: 一、AJAX定义 二、现状与需要解决的问题 三、为什么使用AJAX 四、谁在使用AJAX 五、用AJAX改进你的设计 ... 7.5.1、处理返回的XML  7.5.2、选择合适的XML生成方式  7.5.3、如何在使用XML还是普通文本间权衡

    AJAX开发简略含续一(PDF).rar

    软件介绍 目录: 一、AJAX定义 二、现状与需要解决的问题 三、为什么使用AJAX 四、谁在使用AJAX ... 7.5.1、处理返回的XML  7.5.2、选择合适的XML生成方式  7.5.3、如何在使用XML还是普通文本间权衡

Global site tag (gtag.js) - Google Analytics