Dom4j使用xpath查询带有命名空间的xml文件
方式1
忽略命名空间
java
DocumentFactory factory = DocumentFactory.getInstance();
SAXReader reader = new SAXReader(factory);
Document document = reader.read(xmlFilePath);
Element rootElement = document.getRootElement();
Node node = document.selectSingleNode("//*[local-name()='NOC']/*[local-name()='Section' and @fileRef='nncFireGround.xml']/*[local-name()='NavigationItem' and @idRef='CHDEDCHHA6']");
方式2
定义前缀
java
Map<String,String> map = new HashMap<>();
map.put("s","http://www.boeing.com/FTID-ML");
factory.setXPathNamespaceURIs(map);
XPath xPath = factory.createXPath("/s:NOC/s:Section[@fileRef='nncFireGround.xml']/s:NavigationItem[@idRef='CHDEDCHHA6']");
Node node = xPath.selectSingleNode(document);