阿里 XmlUtils 工具:判断 XML 节点是否存在
在阿里的 XmlUtils 工具中,可以使用以下方法判断是否存在某个节点:
- 使用
selectNodes方法获取指定节点的列表,然后判断列表是否为空。
List<Node> nodeList = XmlUtils.selectNodes(xmlDocument, '//path/to/node');
if (nodeList.isEmpty()) {
// 节点不存在
} else {
// 节点存在
}
- 使用
selectSingleNode方法获取指定节点,然后判断节点是否为空。
Node node = XmlUtils.selectSingleNode(xmlDocument, '//path/to/node');
if (node == null) {
// 节点不存在
} else {
// 节点存在
}
其中 xmlDocument 是一个表示 XML 文档的对象,//path/to/node 是节点的 XPath 表达式,用于定位节点的路径。根据实际情况替换为相应的值。
原文地址: https://www.cveoy.top/t/topic/ooMu 著作权归作者所有。请勿转载和采集!