Dom4j使用xpath查询xml文

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);
相关推荐
readmancynn1 天前
XML_Tomcat_HTTP
xml·http·tomcat
cyt涛1 天前
MyBatis之手动映射
xml·数据库·mybatis·查询·resultmap·手动映射
齐 飞1 天前
使用jackson将xml和对象、List相互转换
xml·java·spring boot·后端·list
代码代码快快显灵1 天前
XML标记语言
xml·java·数据库
Dingdangr2 天前
AndroidManifest.xml文件的重要信息
xml
OEC小胖胖2 天前
MyBatis 如何将 Mapper 接口与其 XML 映射文件关联:深入原理与实现
xml·java·后端·mybatis·web
_Shirley2 天前
android.view.InflateException: Binary XML file line #7: Error inflating class
android·xml·java·ide·kotlin·android studio
陈逸轩*^_^*2 天前
xml重点笔记(尚学堂 3h)
xml·java·笔记·java-ee·intellij-idea
codelife3212 天前
记录word转xml文件踩坑
xml·word