如何使用 java 中的 DOM 区分 XML 单个标签和标签对

How to tell apart XML single tag from tag pair using DOM in java

我正在使用 DOM 解析 XML,我希望能够区分 XML 个文件

<a><b/></a>

<a><b></b></a>

我该怎么做?我知道如何检索 b 标记作为节点,但在 NodeElement class 中似乎没有可用于此任务的方法.

这两种形式被定义为等效的,因此与基于 DOM 的模型无法区分。

规范的相关部分是 http://www.w3.org/TR/REC-xml/#dt-empty

中的定义和相关内容

[Definition: An element with no content is said to be empty.] The representation of an empty element is either a start-tag immediately followed by an end-tag, or an empty-element tag.

规范建议兼容性但不要求 contentspec=EMPTY 元素,例如
使用空元素样式标签,其他使用成对语法,例如http://www.w3.org/TR/REC-xml/#d0e2480