Java 如何确定XML中是否存在标记?

Java 如何确定XML中是否存在标记?,java,xml,Java,Xml,^_^ 我正在使用DOM解析器解析XML数据 我想知道是否存在标签 例如 <a> <aa> <bb> </a> 在这个XML代码中,如果我想知道XML代码中是否存在,我该如何做 我还没找到路..T\u T 如果你知道路的话。。请让我知道 使用SAX解析器 SimpleErrorHandle.java class SimpleErrorHandler implements ErrorHandler { public void w

^_^

我正在使用DOM解析器解析XML数据

我想知道是否存在标签

例如

<a>
  <aa>
  <bb>
</a>

在这个XML代码中,如果我想知道XML代码中是否存在
,我该如何做

我还没找到路..T\u T

如果你知道路的话。。请让我知道

使用SAX解析器 SimpleErrorHandle.java

class SimpleErrorHandler implements ErrorHandler {
    public void warning(SAXParseException e) throws SAXException {
        System.out.println(e.getMessage());
    }

    public void error(SAXParseException e) throws SAXException {
        System.out.println(e.getMessage());
    }

    public void fatalError(SAXParseException e) throws SAXException {
        System.out.println(e.getMessage());
    }
}
使用DOM解析器 输出

The element type "bb" must be terminated by the matching end-tag "</bb>".
元素类型“bb”必须由匹配的结束标记“”终止。
通过XPath:

    XPath path = XPathFactory.newInstance().newXPath();
    final Double count=(Double) path.evaluate("count(//bb)", doc, XPathConstants.NUMBER);
    System.out.println(count.intValue());

你能展示你已经拥有的代码吗?请找到我更新的答案。。还有一个DOM解析器的解决方案。谢谢你的回答。!!^但是我正在使用DOM解析器。。不是SAXParser..T\T
The element type "bb" must be terminated by the matching end-tag "</bb>".
    XPath path = XPathFactory.newInstance().newXPath();
    final Double count=(Double) path.evaluate("count(//bb)", doc, XPathConstants.NUMBER);
    System.out.println(count.intValue());