# 衝突處理 org.w3c.dom **org.w3c.dom 衝突 錯誤訊息** ``` The package org.w3c.dom is accessible from more than one module: <unnamed>, java.xml ``` path 1: jre 11.0.9.11 -> java.xml -> org.w3c.dom.* path 2: jaxen-1.1.6.jar -> org.w3c.dom.* path 3: dom-2.3.0-jaxb-1.0.6.jar -> org.w3c.dom.* 查找後三處有相同的 module,刪除不需要的 Build path 到只剩下一組即可。 **衍伸問題** Dom4j 解析 xml 發生錯誤,如下: ``` java.lang.NoClassDefFoundError: org.jaxen.NamespaceContext ``` 故添加 jaxen-1.1.6.jar。 但會有 org.w3c.dom 衝突,刪除 jaxen-1.1.6.jar 後, 改使用 java.xml 解析。 ### 原程式碼 ```java // 取設定檔屬性Map public static HashMap<String, String> XMLMap(String strType) throws Exception { HashMap<String, String> mapXML = new HashMap<String, String>(); // 取得程式當前路徑 String strDir = System.getProperty("user.dir"); strDir = strDir.replace("\\", "/") + "/"; // 讀取properties.xml放入MAP SAXReader saxReader = new SAXReader(); Document doc = saxReader.read(strDir + "properties.xml"); List<?> listData = doc.selectNodes("properties/propertie[@name='" + strType + "']/item"); for (Object oData : listData) { Element eElement = (Element) oData; mapXML.put(eElement.attributeValue("key"), eElement.attributeValue("value")); } return mapXML; } ``` ### 修改為 ``` java // 取設定檔屬性Map public static HashMap<String, String> XMLMap(String strType) throws Exception { HashMap<String, String> mapXML = new HashMap<String, String>(); // 取得程式當前路徑 String strDir = System.getProperty("user.dir"); File file = new File(strDir + "\\properties.xml"); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); // 建立解析器 Document doc = builder.parse(file); XPath xPath = XPathFactory.newInstance().newXPath(); String expression = "/properties/propertie[@name='" + strType + "']/item"; // 路徑 NodeList nl = (NodeList) xPath.compile(expression).evaluate(doc, XPathConstants.NODESET); // 讀取properties.xml放入MAP for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); Element element = (Element) node; mapXML.put(element.getAttribute("key"), element.getAttribute("value")); } return mapXML; } ``` [reference](/GXst1iw_TYeZNaAaX1QAdw) ###### tags: `Java`