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);
相关推荐
只可远观2 天前
Android XML命令式和Jetpack Compose声明式UI
android·xml
鹏晨互联2 天前
《深入理解 Compose 中的 matchParentSize 与 fillMaxSize —— 从 XML 到 Compose 的对比解析》
xml
小短腿的代码世界2 天前
Qt SVG渲染管线全解析:从XML解析到像素绘制的完整架构设计与性能优化实战
xml·qt·性能优化
HMS工业网络3 天前
技术干货:EtherCAT设备ESI(XML)文件中的CompleteAccess关键字有什么作用
xml·运维·服务器
鹏晨互联4 天前
【Compose vs XML:边框内外间距的实现对比】
android·xml
鹏晨互联5 天前
Jetpack Compose vs XML:fillMaxSize、fillMaxHeight、fillMaxWidth 全面对比
android·xml
如果'\'真能转义说6 天前
OOXML 文档格式剖析:哈希、ZIP结构与识别
xml·算法·c#·哈希算法
ZC跨境爬虫7 天前
跟着 MDN 学 HTML day_34:(深入XML 中的 CDATASection 接口)
xml·前端·html·html5·媒体
hmywillstronger7 天前
【Python】从SAP2000 XML截面库提取数据到Excel
xml·python·excel