请问一下, 如下xml文件:<?xml version="1.0" encoding="UTF-8"?>
<items>
<item1 type="1" />
<item2 type="2" />
<item3 type="3" />
<item4 type="2" />
<item5 type="2" />
<item6 type="2" />
</items>如果要操作 type=3 的条目 是不是只能 先 遍历所有条目 node[i], 然后判断该条目的type 是不是 = 3, 得到i后, 然后才能操作该node?有没有像root.ChildNodes.Nodes['items3'] 这样直接得到该node 的方法?
谢谢
<items>
<item1 type="1" />
<item2 type="2" />
<item3 type="3" />
<item4 type="2" />
<item5 type="2" />
<item6 type="2" />
</items>如果要操作 type=3 的条目 是不是只能 先 遍历所有条目 node[i], 然后判断该条目的type 是不是 = 3, 得到i后, 然后才能操作该node?有没有像root.ChildNodes.Nodes['items3'] 这样直接得到该node 的方法?
谢谢
for i := 0 to total do
begin
if nodes[i].Attributes['type'] = '3' then nodes[i].Attributes['text'] := 'xxxxx';
end;这样遍历的方法, 因为要操作的XML 条目比较多.. 请教好的办法.
谢谢.
<items>
<item1 type="1" />
<item2 type="2" />
<item3 type="3" />
<item4 type="2" />
<item5 type="2" />
<item6 type="2" />
</items>
其中的item1..item6这些一定要区分吗,是否可以都用item来表示呢?用xpath来查找我想可以完成你的需求。当然,一个一个历遍也是一种方法。
<?xml version="1.0" encoding="UTF-8"?>
<items>
<item type="1" />
<item type="2" />
<item type="3">
<name>wudi</name>
</item>
<item type="2" />
<item type="2" />
<item type="2" />
</items>可以用下面代码uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,msxml;//记得uese msxml
procedure TForm1.Button1Click(Sender: TObject);
var
FXMLDoc :IXMLDOMDocument;
xfind : IXMLDOMNode;
xpath : string;
tm : WideString;
begin
fxmldoc := CoDOMDocument.Create;
FXMLDoc.load('d:\dd.xml') ;
xpath := 'item[@type=3]';
xfind := FXMLDoc.documentElement.selectSingleNode(xpath);
if xfind <> nil then
begin
tm :=xfind.selectSingleNode('name').text;
ShowMessage(tm);
end;
FXMLDoc := nil;
end;