ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# Java XPath – 检查节点或属性是否存在? > 原文: [https://howtodoinjava.com/xml/xpath-check-if-xml-tag-exists/](https://howtodoinjava.com/xml/xpath-check-if-xml-tag-exists/) Java 示例**使用 XPath 检查给定 XML 内容中的节点是否存在**或**检查 XML 中的属性是否存在**。 ## 1.如何检查 xml 节点是否存在? 要验证 XML 内容中是否存在节点或标记,您可以针对该 XML 的 DOM 文档执行 xpath 表达式,并计算匹配的节点。 1. `matching nodes > zero` – XML 标记/属性存在。 2. `matching nodes <= zero` – XML 标记/属性不存在。 #### 1.1 XML 文件 ```java <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <employees> <employee id="1"> <firstName>Lokesh</firstName> <lastName>Gupta</lastName> <department> <id>101</id> <name>IT</name> </department> </employee> <employee id="2"> <firstName>Brian</firstName> <lastName>Schultz</lastName> <department> <id>102</id> <name>HR</name> </department> </employee> </employees> ``` #### 1.2 使用 XPath 计数 XML 标签以检查是否存在 ```java package com.howtodoinjava.demo; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Document; import org.w3c.dom.NodeList; public class XPathExample { public static void main(String[] args) throws Exception { // Get DOM Node for XML String fileName = "employees.xml"; Document document = getDocument(fileName); String xpathExpression = ""; // Get all employee names xpathExpression = "/employees/employee/firstName"; System.out.println(checkIfNodeExists(document, xpathExpression)); //true // Get all employee ids xpathExpression = "/employees/employee/@id"; System.out.println(checkIfNodeExists(document, xpathExpression)); //true // Get all employee age xpathExpression = "/employees/employee/@age"; System.out.println(checkIfNodeExists(document, xpathExpression)); //false // Get all department names xpathExpression = "/employees/employee/department/name"; System.out.println(checkIfNodeExists(document, xpathExpression)); //true // Get department locations xpathExpression = "/employees/employee/department/location"; System.out.println(checkIfNodeExists(document, xpathExpression)); //false } private static boolean checkIfNodeExists(Document document, String xpathExpression) throws Exception { boolean matches = false; // Create XPathFactory object XPathFactory xpathFactory = XPathFactory.newInstance(); // Create XPath object XPath xpath = xpathFactory.newXPath(); try { // Create XPathExpression object XPathExpression expr = xpath.compile(xpathExpression); // Evaluate expression result on XML document NodeList nodes = (NodeList) expr.evaluate(document, XPathConstants.NODESET); if(nodes != null && nodes.getLength() > 0) { matches = true; } } catch (XPathExpressionException e) { e.printStackTrace(); } return matches; } private static Document getDocument(String fileName) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(fileName); return doc; } } ``` 程序输出: ```java true true false true false ``` > 阅读更多: [Xpath 示例](https://howtodoinjava.com/xml/java-xpath-expression-examples/) 学习愉快!