test
public class TestOadXmlParse extends TestCase { public static void test() throws Exception { OadCommon.oadCache=OadXMLParse.getOad(); System.out.println(OadCommon.oadCache.get("2112301")); } }
xml
<?xml version="1.0" encoding="UTF-8"?> <root> <mapping id="111" name="222"><!--ID降序排列 --> <!--key:ID,value:标识 --> <property key="2112300" value="00000200"></property> <property key="2112301" value="00100200"></property> <property key="2112302" value="00110200"></property> <property key="2112303" value="00120200"></property> <property key="3101657" value="27090200"></property> </mapping> </root>
parse
package com.protocol.util; import java.io.FileInputStream; import java.io.InputStream; import java.util.LinkedHashMap; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.apache.log4j.Logger; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class OadXMLParse { private static final Logger log = Logger.getLogger("OadXMLParse"); // private static LRUCache<Object, Object> lruCache; private static LinkedHashMap<String, String> dataMap; public static LinkedHashMap<String, String> getOad(String xmlPath) throws Exception { log.info("oad XML mapping path : " + xmlPath); // 1-获取XML-IO流 InputStream xmlInputStream = getXmlInputStream(xmlPath); // 2-解析XML-IO流 ,获取Document 对象,以及Document对象 的根节点 Element rootElement = getRootElementFromIs(xmlInputStream); // 3~5-从根元素解析得到元素 parseElementFromRoot(rootElement); return dataMap; } // 1-获取XML-IO流 private static InputStream getXmlInputStream(String xmlPath) { InputStream inputStream = null; try { // 1-把要解析的 XML 文档转化为输入流,以便 DOM 解析器解析它 inputStream = new FileInputStream(xmlPath); } catch (Exception e) { e.printStackTrace(); } return inputStream; } // 2-解析XML-IO流 ,获取Document 对象,以及Document对象 的根节点 private static Element getRootElementFromIs(InputStream inputStream) throws Exception { if (inputStream == null) { return null; } /* * javax.xml.parsers 包中的DocumentBuilderFactory用于创建DOM模式的解析器对象 , * DocumentBuilderFactory是一个抽象工厂类,它不能直接实例化,但该类提供了一个newInstance方法 , * 这个方法会根据本地平台默认安装的解析器,自动创建一个工厂的对象并返回。 */ // 2-调用 DocumentBuilderFactory.newInstance() 方法得到创建 DOM 解析器的工厂 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); // 3-调用工厂对象的 newDocumentBuilder方法得到 DOM 解析器对象。 DocumentBuilder docBuilder = factory.newDocumentBuilder(); // 4-调用 DOM 解析器对象的 parse() 方法解析 XML 文档,得到代表整个文档的 Document // 对象,进行可以利用DOM特性对整个XML文档进行操作了。 Document doc = docBuilder.parse(inputStream); // 5-得到 XML 文档的根节点 Element root = doc.getDocumentElement(); // 6-关闭流 if (inputStream != null) { inputStream.close(); } return root; } // 3-从根元素解析得到元素 private static void parseElementFromRoot(Element root) { NodeList nl = root.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); if (node instanceof Element) { Element ele = (Element) node; // 4-从元素解析得到属性值 getDataFromElement(ele); // 5-从元素解析特定子元素并解析(以property为例) getCertainElementFromParentElement(ele); } } } // 4-从元素解析得到属性值 private static void getDataFromElement(Element ele) { String id = ele.getAttribute("id");// 根据属性名称读取属性值 String name = ele.getAttribute("name"); // log.info("id == " + id); // log.info("name == " + name); } // 5-从元素解析特定子元素并解析(以property为例) private static void getCertainElementFromParentElement(Element ele) { // cache cize // lruCache = new LRUCache(1000); dataMap = new LinkedHashMap<String, String>(); NodeList propertyEleList = ele.getElementsByTagName("property");// 根据标签名称获取标签元素列表 for (int i = 0; i < propertyEleList.getLength(); i++) { Node node = propertyEleList.item(i); if (node instanceof Element) { Element propertyEle = (Element) node; String key = propertyEle.getAttribute("key"); String value = propertyEle.getAttribute("value"); // log.info("propertyEle: key == " + key); // log.info("propertyEle: value == " + value); // lruCache.put(key, value); //cache 方式 dataMap.put(key, value); //map方式 } } } }
OadCommon
package com.protocol.gb698.oad; import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import org.apache.log4j.Logger; /** * oad 公共类 * * @author YinTao 2020-03-23 */ public class OadCommon { private static final Logger log = Logger.getLogger(OadCommon.class); // public static LRUCache<Object, Object> oadCache; public static LinkedHashMap<String, String> oadCache; /** * 根据key获取对应value * * @param key * @return */ public static byte[] getValue(String key) { Object Value = oadCache.get(key); // Object Value = dotaMap.get(key); log.info("key : " + key + ", value :" + Value.toString()); byte[] b = StrUtil.HexString2Bytes(Value.toString()); return b; } /** * get all key * * @return */ public static List<String> getALLKeys() { List<String> list = new ArrayList<String>(); Iterator<?> iter = oadCache.entrySet().iterator(); while (iter.hasNext()) { Map.Entry entry = (Map.Entry) iter.next(); // log.info("key= " + entry.getKey() + " and value= " + entry.getValue()); list.add(entry.getKey().toString()); } return list; } }