这个XML文件怎么读?(50分)

A

aloze

Unregistered / Unconfirmed
GUEST, unregistred user!
<xbkf ver='V6.5' mode="request">
<command>MSG</command>
<id>120</id>
<shift>3</shift>
<empno>999</empno>
</xbkf>
 
T

tylerzhang

Unregistered / Unconfirmed
GUEST, unregistred user!
procedure TForm1.Button3Click(Sender: TObject);
var
strFilePath:string;
rootNode,node1,node2 :IXMLNode;
begin
try
strFilePath:=ExtractFilePath(Application.ExeName) + 'config.xml';
XMLDocument1.LoadFromFile(strFilePath);
XMLDocument1.Active:= True;
//use XMLDoc here
Node1 := XMLDocument1.DocumentElement.ChildNodes.FindNode('dispName');
node2 := XMLDocument1.DocumentElement.ChildNodes.FindNode('code');
showmessage(node2.Text );
showmessage(node1.Text );
XMLDocument1.Active := False;
finally
XMLDocument1 := nil;
end;

end;
 
W

wlh_1

Unregistered / Unconfirmed
GUEST, unregistred user!
//自己写的函数为读取一个指定的xml文件内容,包括自动读取属性和属性值。将结果写到一个memo里。wlh 080328
//合适2级结构
function getxml(xmlfilename:string;xml:Txmldocument;memo:Tmemo):string;
var xmlnode1,rootnode:Ixmlnode;
i,j,m,n,p:integer;
nodename:string;
begin
memo.Clear ;
xml.FileName:=xmlfilename;
xml.Active:=true;
i:=0;
rootnode:= xml.DocumentElement;
xmlnode1:= xml.DocumentElement;
j:=rootnode.ChildNodes.Count;//根下有多少个一级节点
for i:=0 to rootnode.ChildNodes.Count- 1do

begin
n:=rootnode.ChildNodes.ChildNodes.Count ;//每个节点下有多少属性项
for p := 0 to n- 1do
begin
xmlnode1:=rootnode.ChildNodes.ChildNodes[p];
nodename:=xmlnode1.NodeName ;//属性项的名称
memo.Lines.Add((nodename+': '+xmlnode1.Text));//属性值

end;
memo.Lines.Add('------------------------------------------------------------------') ;

end;
end;

类似如下结构:
<NewDataSet> //根
//一级节点1
- <Table>
<Country>China</Country> //节点下属性1名称和值
<City>Beijing</City> //节点下属性2名称和值
</Table>
//一级节点2
- <Table>
<Country>China</Country>
<City>Hohhot</City>
</Table>
.....
 
L

liyinwei

Unregistered / Unconfirmed
GUEST, unregistred user!
假设 XML 文件的内容如下
<?xml version="1.0" encoding="gb2312" ?>
<xbkf ver='V6.5' mode="request">
<command>MSG</command>
<id>120</id>
<shift>3</shift>
<empno>999</empno>
</xbkf>
代码如下:
procedure TForm1.Button1Click(Sender: TObject);
var
rNode,cNode: IXMLNode;
i: Integer;
begin
XMLDocument1.LoadFromFile('c:/1.xml');
XMLDocument1.Active := True;
rNode := XMLDocument1.Node;
rNode := rNode.ChildNodes[1];
Memo1.Lines.Add('ver=' + rNode.AttributeNodes['ver'].NodeValue);
Memo1.Lines.Add('mode=' + rNode.AttributeNodes['mode'].NodeValue);
for i := 0 to rNode.ChildNodes.Count - 1do
begin
cNode := rNode.ChildNodes;
Memo1.Lines.Add(cNode.NodeName + '=' + cNode.NodeValue);
end;
end;
 
A

angelaqazwsx

Unregistered / Unconfirmed
GUEST, unregistred user!
文件不大用dom
文件大还是建议用sax
 

地质灾害

Unregistered / Unconfirmed
GUEST, unregistred user!
请问楼上的:SAX是什么?
 

Similar threads

回复
0
查看
846
不得闲
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
875
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
顶部