package com.gd.xml.parse; import java.io.File; import java.io.InputStream; import org.dom4j.Document; /** * xml解析接口 * @author sandy * */ public interface XmlParser { /** * 根据文档对象doc来解析xml * @param doc * @return */ public abstract XmlParser parseXml(Document doc); /** * 根据传入的xml输入流解析xml * @param is * @return */ public abstract XmlParser parseXml(InputStream is); /** * 根据传入的文件来解析xml * @param xmlfile * @return */ public abstract XmlParser parseXml(File xmlfile); /** * 根据文件名来解析xml * @param xmlfileName * @return */ public abstract XmlParser parseXml(String xmlfileName); }
/** * */ package com.gd.xml.parse; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.util.List; import java.util.Map; import java.util.TreeMap; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.Node; import org.dom4j.XPath; import org.dom4j.io.SAXReader; /** * @author sandy * */ public class ConfigValuesXmlParse implements XmlParser { private Document document=null; private Map<String,ValueSet> valueSetMap=null; InputStream is=null; /* (non-Javadoc) * @see com.gd.xml.parse.XmlParser#parseXml(org.dom4j.Document) */ @SuppressWarnings("unchecked") public ConfigValuesXmlParse() { valueSetMap=new TreeMap<String,ValueSet>(); } public XmlParser parseXml(Document doc){ this.document=doc; XPath path=doc.createXPath("/configValues//valueSet"); List<Node> nodes=path.selectNodes(doc); for(Node valueSetNode:nodes){ ValueSetDefaultImpl valueset=new ValueSetDefaultImpl(); Element valueSetE=(Element)valueSetNode; valueset.setId(valueSetE.attributeValue("id")); valueset.setName(valueSetE.attributeValue("name")); path=valueSetE.createXPath(".//value"); nodes=path.selectNodes(valueSetE); for(Node valueNode:nodes){ Value value=new Value(); Element valueE=(Element)valueNode; value.setDisplay(valueE.attributeValue("display")); value.setValue(valueE.attributeValue("value")); valueset.getValues().add(value); } this.valueSetMap.put(valueset.getId(), valueset); } path=this.document.createXPath("//dynamicValueSet"); nodes=path.selectNodes(doc); for(Node dyNode:nodes){ DynamicValueSet dyvalueSet=new DynamicValueSet(); Element dyElement=(Element)dyNode; dyvalueSet.setId(dyElement.attributeValue("id")); dyvalueSet.setName(dyElement.attributeValue("name")); dyvalueSet.setSql(dyElement.getTextTrim()); this.valueSetMap.put(dyvalueSet.getId(),dyvalueSet); } return this; } /* (non-Javadoc) * @see com.gd.xml.parse.XmlParser#parseXml(java.io.InputStream) */ public XmlParser parseXml(InputStream is){ SAXReader saxReader = new SAXReader(); Document doc=null; try { doc = saxReader.read(is); } catch (DocumentException e) { //add log message ..................... throw new RuntimeException(e.getMessage()); } return this.parseXml(doc); } /* (non-Javadoc) * @see com.gd.xml.parse.XmlParser#parseXml(java.io.File) */ public XmlParser parseXml(File xmlfile){ try { is=new FileInputStream(xmlfile); } catch (FileNotFoundException e) { System.out.println("文件名字为:"+"["+xmlfile.getName()+"]"+"的文件找不到!"); //e.printStackTrace(); } return this.parseXml(is); } /* (non-Javadoc) * @see com.gd.xml.parse.XmlParser#parseXml(java.lang.String) */ public XmlParser parseXml(String xmlfileName){ ClassLoader clazzloader=Thread.currentThread().getContextClassLoader(); is=clazzloader.getResourceAsStream(xmlfileName); return this.parseXml(is); } public static void main(String[] args) { XmlParser s=new ConfigValuesXmlParse(); s.parseXml("configValues.xml"); } /** * @return the valueSetMap */ public Map<String, ValueSet> getValueSetMap() { return valueSetMap; } }
/** * */ package com.gd.xml.parse; import java.util.List; /** * @author sandy * */ public interface ValueSet{ public List<Value> getValues(); }
/** * */ package com.gd.xml.parse; /** * @author sandy * */ public class Value { private String display=null; private String value=null; /** * @return the display */ public String getDisplay() { return display; } /** * @param display the display to set */ public void setDisplay(String display) { this.display = display; } /** * @return the value */ public String getValue() { return value; } /** * @param value the value to set */ public void setValue(String value) { this.value = value; } @Override public String toString() { String msg = "{"; msg += "value[" + value + "], "; msg += "display[" + display + "]}"; return msg; } }
/** * */ package com.gd.xml.parse; import java.util.LinkedList; import java.util.List; import java.util.Map; import com.gd.xml.parse.util.QueryDao; /** * @author sandy * */ public class DynamicValueSet implements ValueSet{ private String id=null; private String name=null; private String sql=null; private List<Value> values = new LinkedList<Value>(); /** * @return the id */ public String getId() { return id; } /** * @param id the id to set */ public void setId(String id) { this.id = id; } /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } /** * @return the sql */ public String getSql() { return sql; } /** * @param sql the sql to set */ public void setSql(String sql) { this.sql = sql; } /** * @return the values */ public List<Value> getValues() { List<Value> list = new LinkedList<Value>(); QueryDao dao = new QueryDao(); Map<String, String> map = dao.findDisplayValueSet(sql); for (String key : map.keySet()) { Value val = new Value(); val.setValue(key); val.setDisplay(map.get(key)); list.add(val); } return list; } }
/** * */ package com.gd.xml.parse; import java.util.LinkedList; import java.util.List; /** * @author sandy * */ public class ValueSetDefaultImpl implements ValueSet{ private String id=null; private String name=null; private List<Value> values = new LinkedList<Value>(); /** * @return the id */ public String getId() { return id; } /** * @param id the id to set */ public void setId(String id) { this.id = id; } /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } /** * @param values the values to set */ public void setValues(List<Value> values) { this.values = values; } /** * @return the values */ public List<Value> getValues() { return values; } }
/** * */ package com.gd.xml.parse.util; import java.util.HashMap; import java.util.Map; /** * @author sandy * */ public class QueryDao { public Map<String,String> findDisplayValueSet(String sql){ Map<String,String> map=new HashMap<String,String>(); /* *<dynamicValueSet id="DGV001" name="卡BIN"><![CDATA[ * SELECT code_vl,code_name FROM XXXXX * ]]></dynamicValueSet> * sql=SELECT code_vl,code_name FROM XXXXX */ //根据从数据库中查询获得的数据,来构建map对象 return map; } }
<?xml version="1.0" encoding="UTF-8"?> <configValues> <valueSet id="GV0001" name="交易地点"> <value display="北京" value="01" /> <value display="上海" value="02" /> <value display="天津" value="03" /> <value display="山东" value="04" /> <value display="山西" value="05" /> </valueSet> <valueSet id="GV0002" name="转账种类"> <value display="活期转账" value="01" /> <value display="定活互转" value="02" /> <value display="向企业转账" value="03" /> <value display="贷记卡转账" value="04" /> </valueSet> <valueSet id="GV0003" name="卡种"> <value display="信用卡" value="01" /> <value display="借记卡" value="02" /> </valueSet> <valueSet id="GV0004" name="卡别"> <value display="易行卡" value="01" /> <value display="CBA卡" value="02" /> <value display="公务员卡" value="03" /> <value display="学生卡" value="04" /> <value display="婚庆卡" value="05" /> <value display="社区卡" value="06" /> <value display="青年卡" value="07" /> <value display="车主卡" value="08" /> <value display="理财卡" value="09" /> <value display="交通卡" value="10" /> </valueSet> <valueSet id="GV0005" name="客户等级"> <value display="沃德客户" value="01" /> <value display="交银理财客户" value="02" /> <value display="普通客户" value="03" /> <value display="VIP客户" value="04" /> </valueSet> <valueSet id="GV0006" name="员工类型"> <value display="行内员工" value="01" /> <value display="非行内员工" value="02" /> </valueSet> <valueSet id="GV0007" name="客户性别"> <value display="女性" value="01" /> <value display="男性" value="02" /> </valueSet> <valueSet id="GV008" name="客户等级"> <value display="普通客户" value="01" /> <value display="VIP用户" value="02" /> </valueSet> <dynamicValueSet id="DGV001" name="卡BIN"><![CDATA[ SELECT code_vl,code_name FROM XXXXX ]]></dynamicValueSet> <dynamicValueSet id="DGV002" name="借记卡类型"><![CDATA[ SELECT DCCDKD,DCCDNM FROM XXXXX ]]></dynamicValueSet> <dynamicValueSet id="DGV005" name="交易类型"><![CDATA[ SELECT code_vl,code_name FROM XXXXX ]]></dynamicValueSet> </configValues>
/** * */ package com.gd.xml.parse.test; import java.util.List; import java.util.Map; import com.gd.xml.parse.*; /** * @author sandy * */ public class Client { /** * @param args */ public static String XML_PARSERFILENAME="configValues.xml"; public static void main(String[] args) { Map<String, ValueSet> valuesetMap=init(); fetchValues(valuesetMap); } private static void fetchValues(Map<String, ValueSet> valuesetMap) { ValueSet valset=valuesetMap.get("GV0001"); ValueSetDefaultImpl valuesetimpl=(ValueSetDefaultImpl)valset; System.out.print("ID:"+"["+valuesetimpl.getId()+"]"+"Name:["+valuesetimpl.getName()+"]"+"\n\r"); List<Value> values=valuesetimpl.getValues(); for(Value value:values){ System.out.println("display:["+value.getDisplay()+"]"+"value:["+value.getValue()+"]"); } } private static Map<String,ValueSet> init() { XmlParser parser=new ConfigValuesXmlParse(); ConfigValuesXmlParse cfgparser=(ConfigValuesXmlParse)parser.parseXml(XML_PARSERFILENAME); return cfgparser.getValueSetMap(); } }