0
点赞
收藏
分享

微信扫一扫

校园美食探索者:Spring Boot开发的分享平台

12a597c01003 2024-09-23 阅读 32
xml

Dom4j使用xpath查询带有命名空间的xml文件

方式1

忽略命名空间

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

定义前缀

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);
举报

相关推荐

0 条评论