iNode.selectSingleNode('@abc).text := 'abc123';
这句在iNode 中有 abc时才有效.如何当没有abc的时候添加 attribute?还有一个问题
取 iNode.selectSingleNode('@abc').text 时,
是不是必须判断
iNode.selectSingleNode('@abc') = nil有没有办法 iNode.selectSingleNode('@abc') = nil 时, iNode.selectSingleNode('@abc').text 自动= '', 而不出错.谢谢.
这句在iNode 中有 abc时才有效.如何当没有abc的时候添加 attribute?还有一个问题
取 iNode.selectSingleNode('@abc').text 时,
是不是必须判断
iNode.selectSingleNode('@abc') = nil有没有办法 iNode.selectSingleNode('@abc') = nil 时, iNode.selectSingleNode('@abc').text 自动= '', 而不出错.谢谢.
如Delphi带的Example
//Turbo Delphi Help[ms-help://borland.bds4/bds4win32devguide/html/xmldocworkingwithxmlnodes.htm]BorlandStock := XMLDocument1.DocumentElement.ChildNodes[0];
BorlandStock.ChildNodes['shares'].Attributes['type'] := 'common';
这儿如果不存在type属性,则自动添加type=commonBorlandStock.ChildNodes['shares'].Attributes['type'] := nil;
这样删除type属性
if iNode.selectSingleNode('//@abc) <> Nil then
iNode.selectSingleNode('//@abc).text := 'abc123';
var
Root,Node:IXMLDOMNode;
XMLFile:IXMLDOMDocument;
begin
XMLFile:=CreateDOMDocument();
XMLFile.load('d:\test.xml');
root:=XMLFile.documentElement;
Node:=Root.selectSingleNode('/abc');
if Node <> Nil then
Node.text := 'abc123'
else
begin
Node:=XMLFile.createNode(1,'abc','');
Node.text:='abc123';
Root.appendChild(Node);
XMLFile.save('d:\test.xml');
end;
end;
var
attr: IXMLDOMNode;
begin
attr := node.selectSingleNode(attrName);
if (value=Nil) then
begin
处理置空情况
exit;
end; if (attr=nil) then
begin
attr := node.ownerDocument.createNode(attrName);
Node.appendChild(attr);
end;
attr.text := value;
end;