<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??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/) 學習愉快!
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看