我有一个本应定位基于属性值的节点在传送一个子节点一些简单的处理XML代码:这些名为“#text”的XML节点发生了什么?
function GetNodeByAttributeValue(
const AParentNode: IXMLNode;
const AttributeName: string; AttributeValue: Variant): IXMLNode;
var
i: integer;
value: Variant;
begin
result := nil;
if (not Assigned(AParentNode)) or (AttributeName = '') then
exit;
for i := 0 to AParentNode.ChildrenCount-1 do
begin
result := AParentNode.Children[i];
value := result.GetAttributeValue(AttributeName, UnAssigned);
if not VarIsEmpty(value) then
exit;
end;
result := nil;
end;
非常简单的,对不对?但是,当我尝试运行此操作时,在某些情况下它会因访问冲突而崩溃。以下是发生了什么事情:
IXML *实现由RemObjects SDK库提供。 result.GetAttributeValue
电话uROMSXMLImpl.TROMSXMLNode.GetAttributeValue
,这就要求TROMSXMLNode.GetAttributeByName
,它说
node := fNode.attributes.getNamedItem(anAttributeName);
而这种崩溃是因为fNode.attributes
回报零。据我了解,这不应该发生。
奇怪的是,回到原始函数中的for循环,AParentNode.ChildrenCount
返回3.但原始XML文档中的节点只有一个子节点。它符合我正在寻找的标准。
<ParentNode>
<namespace:ChildNode name="right-name">
但AParentNode.ChildrenCount
返回3.我在调试器中打开它们,并得到这个:
AParentNode.Children[0].name: '#text'
AParentNode.Children[1].name: 'namespace:ChildNode'
AParentNode.Children[2].name: '#text'
在世界上什么是这些 “#text” 节点?它们不在XML文档中,我没有编写任何代码来插入它们。他们为什么在那里,为什么他们是越野车,有什么我可以做的,以防止他们搞砸我的属性搜索?