我有一個本應定位基於屬性值的節點在傳送一個子節點一些簡單的處理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文檔中,我沒有編寫任何代碼來插入它們。他們爲什麼在那裏,爲什麼他們是越野車,有什麼我可以做的,以防止他們搞砸我的屬性搜索?