0

我有一个像这样的xml:

<a>
  <b>1</b>
  <c>2</c>
  <d>3</d>
</a>

以及一个解析QDomDocument包装它的递归函数。该函数对s进行迭代QDomNode,将其转换为QDomElements并调用text()方法获取数据。不幸的是,也QDomElement::text()可以在<a>水平上工作并返回:123. 所以它收集了所有嵌套元素的文本。
我希望它返回一个空字符串 bcs,我宁愿不检查tagName()值,因为它们可能很多。所以我宁愿通过haveng/没有文本来检查节点标签,反之亦然。这是可行的吗?是否有一种方法可以<a>为级别返回空字符串和文本值<b>, <c>, <d>
PSQDomNode::nodeValue()为所有元素返回一个空文本。

4

1 回答 1

0

看来我错了,因为我没有迭代QDomNode无法转换为QDomElements 的 s。根据这个答案

这是 DOM 规范要求的:

The Text interface represents the textual content (termed character data in XML) of an Element or Attr. If there is no markup
inside an element's content, the text is contained in a single object
implementing the Text interface that is the only child of the element.
If there is markup, it is parsed into a list of elements and Text
nodes that form the list of children of the element.

<b>我在-like 元素中没有标记。所以在<b>元素级别我有el.childNodes().size() == 1,el.firstChild().isElement() == falseel.firstChild().nodeValue()返回一个非空值!

于 2021-07-25T13:24:18.203 回答