【发布时间】:2012-08-28 19:40:44
【问题描述】:
鉴于下面的 XML 示例;
- 如何轻松检查给定对象是否存在?
- 如何轻松添加组或用户类型的项目? (加一整块)
<role>
<access>
<control>
<type>group</type>
<object>COMPUTER\Administrators</object>
</control>
<control>
<type>user</type>
<object>COMPUTER\Admin</object>
</control>
</access>
</role>
代码:
var
Doc: IXMLDOMDocument2;
Node: IXMLDOMNode;
procedure Test;
begin
Doc := CreateOleObject('Microsoft.XMLDOM') as IXMLDomDocument2;
Doc.load('test.xml');
// This Works
Node := Doc.selectSingleNode('//role/access/control');
// But this does not work:
Node := Doc.selectSingleNode('//role/access/control[type = ''group'']');
// EDIT: This does work, but how to combine with object=COMPUTER\Admin?
Node := Doc.selectSingleNode('//role/access/control[type="group"]');
// EDIT: This does not work either
Node := Doc.selectSingleNode('//role/access/control[type="group" and object="COMPUTER\Administrators"]');
end;
【问题讨论】:
-
@Adam:+1 好点,这是我的错误,所以我用正确的截图替换了截图。
-
你们为什么要发布文本文件的截图?只是好奇...
-
@Adam,因为他们不应该这样。 :-) XML 应该作为文本内容发布(并且正确格式化也不难),以便想要在发布之前检查答案的人可以复制和粘贴它。
-
@Adam 出于某种原因,即使我将其标记为代码也无法正确显示
-
add an item of type group or user 是什么意思?您的意思是,如何添加整个
<control>节点或仅将单个<type>添加到现有的<control>?
标签: xml delphi delphi-2010 ixmldomdocument