【发布时间】:2011-12-07 14:31:26
【问题描述】:
我们正在使用 Delphi 2007 和 oxmldom Open XML 提供程序。
正常情况下的输入文件如下所示:
<root>
<child>Some Text</child>
</root>
现在我们必须处理一个使用 CDATA 节点类型的输入文件:
<root>
<child><![CDATA[Some special Text]]></child>
</root>
Node.IsTextElement 突然返回False,但Node.Text 仍按预期工作。
我知道IXMLNode.IsTextElement 只是一种方便的方法,但我觉得这种行为很奇怪。
作为一种解决方法,我们现在使用这种自定义方法:
class function TXmlUtils.IsTextOrCDataElement(ANode: IXMLNode): Boolean;
begin
Result := False;
if ANode.ChildNodes.Count = 0 then begin
if ANode.NodeType in [ntText, ntCData] then begin
Result := True;
end;
end else
if ANode.ChildNodes.Count = 1 then begin
if ANode.ChildNodes.First.NodeType in [ntText, ntCData] then begin
Result := True;
end;
end;
end;
我的问题是:为什么IsTextElement 不适用于CDATA 节点,有没有更简单的解决方法?
【问题讨论】:
-
但是... CDATA 元素不是 TEXT 元素,所以这种行为是正确的。您的解决方法似乎根本不是解决方法,而是实现了您期望的正确行为。
-
@RitsaertHornstra:您应该将其发布为答案以获得荣誉。
-
所以
Text属性适用于不同类型的节点,而IsTextElement只查找“TEXT”类型的节点?对吗? -
但是@Ritsaert,IsTextElement 方法 also 对于非文本节点返回 True。具体来说,对于具有单个文本节点子节点的元素,它返回 True,例如上面示例中的
<child>。因此,由于它已经不同于简单的is或Supports测试,Jens 关于为什么它不为 other 类似文本的节点返回 True 的问题是合理的。 -
@Rob Kennedy - 在
IsTextElement中有Element字。是 Element,其中包含Text节点。从字面上看,对我来说,这个功能一切都很好。
标签: xml delphi delphi-2007