XML 文件读取问题 ( 积分: 300 )

  • 主题发起人 主题发起人 zhm_good
  • 开始时间 开始时间
Z

zhm_good

Unregistered / Unconfirmed
GUEST, unregistred user!
XML文件为:
<?xml version="1.0" encoding="UTF-8"?>
<entity-config>
<RecordFile>
<SysRecord>
<Record File="e:/11/2005-05-21/11&1-38-5.mp4"/>
<Record File="e:/12/2005-05-21/12&1-38-5.mp4"/>
<Record File="e:/11/2005-05-21/11&1-39-5.mp4"/>
<Record File="e:/12/2005-05-21/12&1-39-6.mp4"/>
<Record File="e:/11/2005-05-21/11&1-51-3.mp4"/>
<Record File="e:/12/2005-05-21/12&1-51-3.mp4"/>
<Record File="e:/11/2005-05-21/11&14-39-50.mp4"/><Record File="e:/12/2005-05-21/12&14-39-51.mp4"/><Record File="c:/2323"/></SysRecord>
<annunciatorRecord>
<data File="d:/sd"/>
</annunciatorRecord>
<TVRecord>
<data File="d:/sd"/>
</TVRecord>
</RecordFile>
<MHSet>
<SQLServer>
<SQLServer Ip="192.168.1.1" UserName="sa1" Password="sa"/>
<SQLServer Ip="192.168.1.2" UserName="sa1" Password="sa"/>
</SQLServer>
<FTP Ip="192.168.1.253" UserName="ftp" Password="ftp"/>
<APPServer Ip="10.13.1.23"/>
</MHSet>
</entity-config>
代码如下:
下面这段代码在运行时是没有问题的。
procedure TMHconfig.Button3Click(Sender: TObject);

function getnodefromIXMLNodeList(childnodes:IXMLNodeList;nodename:String):IXMLNode;
var
i: Integer;
begin
for i :=1 to childnodes.Count do begin
if(childnodes.Get(i-1).NodeName = nodename) then begin
result:= childnodes[i-1];
exit;
end;
end;
end;

var
xmlDocument :IXMLDocument;
Node :IXMLNode;

XmlnodeList :TStrings;
i :Integer;
urlcount :Integer;
begin
//xml节点路径
xmlnodeList:=TStringList.Create;
xmlnodeList.Delimiter:='.';
xmlnodeList.DelimitedText:='entity-config.RecordFile.SysRecord';
urlcount:=xmlnodeList.Count;
//xml对象
xmlDocument :=TXMLDocument.Create(nil);
xmlDocument.LoadFromFile(StrFileName);
xmlDocument.Active:=true;
try
node:= xmlDocument.DocumentElement;
if(node.NodeName = xmlnodeList[0]) then
begin
//扫描节点
for i := 1 to urlcount-1 do
begin
if(node <> nil) then
node := getnodefromIXMLNodeList(node.ChildNodes,xmlnodeList)
else Break ;
end;

if(node = nil)then
begin
exit;
end
else
begin
for i :=0 to node.ChildNodes.Count-1 do
Memo1.Lines.Add(node.ChildNodes.Nodes.Attributes['File']);
node.ChildNodes.Delete(0);
// node := CreateNode(XMLNode,'record','');
node := node.AddChild('Record');
node.SetAttribute('File','c:/2323');
xmlDocument.SaveToFile(StrFileName);

end;

end;


except
// result:='error';
end;
xmlDocument.Active:=false;
end;

能够在 memo1里显示正确内容
e:/11/2005-05-21/11&1-38-5.mp4
e:/12/2005-05-21/12&1-38-5.mp4
e:/11/2005-05-21/11&1-39-5.mp4
e:/12/2005-05-21/12&1-39-6.mp4
e:/11/2005-05-21/11&1-51-3.mp4
e:/12/2005-05-21/12&1-51-3.mp4
e:/11/2005-05-21/11&14-39-50.mp4
e:/12/2005-05-21/12&14-39-51.mp4
c:/2323

但是我如果改造成下面这样,就有问题了:

unit zhmsys;

interface


uses Windows, Messages, SysUtils, Variants, Classes,XMLIntf,XMLDoc ;


type

TNormal= class
private
FXmlDocument: TXMLDocument;
FXmlNode: IXMLNode;
FXmlFileName: String;
procedure SetXmlDocument(const Value: TXMLDocument);
procedure SetXmlNode(const Value: IXMLNode);

public
constructor create ;
destructor destroy; override;
property XmlDocument :TXMLDocument read FXmlDocument write SetXmlDocument;
property XmlNode :IXMLNode read FXmlNode write SetXmlNode;
Function XmlNodeReFresh(XmlFilename,NodePath : String):Boolean;
procedure Xmlsave();
end;

PNormal=^TNormal;

var
Sys:PNormal;
implementation

{ TNormal }

constructor TNormal.create;
begin
FXmlDocument := TXMLDocument.Create(nil);

end;

destructor TNormal.destroy;
begin

inherited;
end;

procedure TNormal.SetXmlDocument(const Value: TXMLDocument);
begin
FXmlDocument := Value;
end;

procedure TNormal.SetXmlNode(const Value: IXMLNode);
begin
FXmlNode := Value;
end;

function TNormal.XmlNodeReFresh(XmlFilename, NodePath: String): Boolean;

function getnodefromIXMLNodeList(childnodes:IXMLNodeList;nodename:String):IXMLNode;
var
i: Integer;
begin
for i :=1 to childnodes.Count do begin
if(childnodes.Get(i-1).NodeName = nodename) then begin
result:= childnodes[i-1];
exit;
end;
end;
end;

var
XmlnodeList :TStrings;
i :Integer;
urlcount :Integer;
begin
//xml节点路径
FXmlFileName := XmlFilename ;
xmlnodeList:=TStringList.Create;
xmlnodeList.Delimiter:='.';
xmlnodeList.DelimitedText := NodePath ; //
urlcount:=xmlnodeList.Count;
try
xmlDocument.LoadFromFile(XmlFilename);
xmlDocument.Active:=true;
try
XmlNode := xmlDocument.DocumentElement;  //就是这句有问题!!!!
if(XmlNode.NodeName = xmlnodeList[0]) then
begin
for i := 1 to urlcount-1 do //扫描节点
begin
if(XmlNode <> nil) then
XmlNode := getnodefromIXMLNodeList(XmlNode.ChildNodes,xmlnodeList)
else Break ;
end;
end;


except
// MessageBox()
end;
finally
// xmlDocument.Active:=false;
end;
Result := (XmlNode<> nil) and (i=urlcount) ;


xmlnodeList.free;

end;

procedure TNormal.Xmlsave;
begin
XmlDocument.SaveToFile(FXmlFileName);
end;

end.

程序运行到 : XmlNode := xmlDocument.DocumentElement; 就报读地址错了。
???????
 
XML文件为:
<?xml version="1.0" encoding="UTF-8"?>
<entity-config>
<RecordFile>
<SysRecord>
<Record File="e:/11/2005-05-21/11&1-38-5.mp4"/>
<Record File="e:/12/2005-05-21/12&1-38-5.mp4"/>
<Record File="e:/11/2005-05-21/11&1-39-5.mp4"/>
<Record File="e:/12/2005-05-21/12&1-39-6.mp4"/>
<Record File="e:/11/2005-05-21/11&1-51-3.mp4"/>
<Record File="e:/12/2005-05-21/12&1-51-3.mp4"/>
<Record File="e:/11/2005-05-21/11&14-39-50.mp4"/><Record File="e:/12/2005-05-21/12&14-39-51.mp4"/><Record File="c:/2323"/></SysRecord>
<annunciatorRecord>
<data File="d:/sd"/>
</annunciatorRecord>
<TVRecord>
<data File="d:/sd"/>
</TVRecord>
</RecordFile>
<MHSet>
<SQLServer>
<SQLServer Ip="192.168.1.1" UserName="sa1" Password="sa"/>
<SQLServer Ip="192.168.1.2" UserName="sa1" Password="sa"/>
</SQLServer>
<FTP Ip="192.168.1.253" UserName="ftp" Password="ftp"/>
<APPServer Ip="10.13.1.23"/>
</MHSet>
</entity-config>
代码如下:
下面这段代码在运行时是没有问题的。
procedure TMHconfig.Button3Click(Sender: TObject);

function getnodefromIXMLNodeList(childnodes:IXMLNodeList;nodename:String):IXMLNode;
var
i: Integer;
begin
for i :=1 to childnodes.Count do begin
if(childnodes.Get(i-1).NodeName = nodename) then begin
result:= childnodes[i-1];
exit;
end;
end;
end;

var
xmlDocument :IXMLDocument;
Node :IXMLNode;

XmlnodeList :TStrings;
i :Integer;
urlcount :Integer;
begin
//xml节点路径
xmlnodeList:=TStringList.Create;
xmlnodeList.Delimiter:='.';
xmlnodeList.DelimitedText:='entity-config.RecordFile.SysRecord';
urlcount:=xmlnodeList.Count;
//xml对象
xmlDocument :=TXMLDocument.Create(nil);
xmlDocument.LoadFromFile(StrFileName);
xmlDocument.Active:=true;
try
node:= xmlDocument.DocumentElement;
if(node.NodeName = xmlnodeList[0]) then
begin
//扫描节点
for i := 1 to urlcount-1 do
begin
if(node <> nil) then
node := getnodefromIXMLNodeList(node.ChildNodes,xmlnodeList)
else Break ;
end;

if(node = nil)then
begin
exit;
end
else
begin
for i :=0 to node.ChildNodes.Count-1 do
Memo1.Lines.Add(node.ChildNodes.Nodes.Attributes['File']);
node.ChildNodes.Delete(0);
// node := CreateNode(XMLNode,'record','');
node := node.AddChild('Record');
node.SetAttribute('File','c:/2323');
xmlDocument.SaveToFile(StrFileName);

end;

end;


except
// result:='error';
end;
xmlDocument.Active:=false;
end;

能够在 memo1里显示正确内容
e:/11/2005-05-21/11&1-38-5.mp4
e:/12/2005-05-21/12&1-38-5.mp4
e:/11/2005-05-21/11&1-39-5.mp4
e:/12/2005-05-21/12&1-39-6.mp4
e:/11/2005-05-21/11&1-51-3.mp4
e:/12/2005-05-21/12&1-51-3.mp4
e:/11/2005-05-21/11&14-39-50.mp4
e:/12/2005-05-21/12&14-39-51.mp4
c:/2323

但是我如果改造成下面这样,就有问题了:

unit zhmsys;

interface


uses Windows, Messages, SysUtils, Variants, Classes,XMLIntf,XMLDoc ;


type

TNormal= class
private
FXmlDocument: TXMLDocument;
FXmlNode: IXMLNode;
FXmlFileName: String;
procedure SetXmlDocument(const Value: TXMLDocument);
procedure SetXmlNode(const Value: IXMLNode);

public
constructor create ;
destructor destroy; override;
property XmlDocument :TXMLDocument read FXmlDocument write SetXmlDocument;
property XmlNode :IXMLNode read FXmlNode write SetXmlNode;
Function XmlNodeReFresh(XmlFilename,NodePath : String):Boolean;
procedure Xmlsave();
end;

PNormal=^TNormal;

var
Sys:PNormal;
implementation

{ TNormal }

constructor TNormal.create;
begin
FXmlDocument := TXMLDocument.Create(nil);

end;

destructor TNormal.destroy;
begin

inherited;
end;

procedure TNormal.SetXmlDocument(const Value: TXMLDocument);
begin
FXmlDocument := Value;
end;

procedure TNormal.SetXmlNode(const Value: IXMLNode);
begin
FXmlNode := Value;
end;

function TNormal.XmlNodeReFresh(XmlFilename, NodePath: String): Boolean;

function getnodefromIXMLNodeList(childnodes:IXMLNodeList;nodename:String):IXMLNode;
var
i: Integer;
begin
for i :=1 to childnodes.Count do begin
if(childnodes.Get(i-1).NodeName = nodename) then begin
result:= childnodes[i-1];
exit;
end;
end;
end;

var
XmlnodeList :TStrings;
i :Integer;
urlcount :Integer;
begin
//xml节点路径
FXmlFileName := XmlFilename ;
xmlnodeList:=TStringList.Create;
xmlnodeList.Delimiter:='.';
xmlnodeList.DelimitedText := NodePath ; //
urlcount:=xmlnodeList.Count;
try
xmlDocument.LoadFromFile(XmlFilename);
xmlDocument.Active:=true;
try
XmlNode := xmlDocument.DocumentElement;  //就是这句有问题!!!!
if(XmlNode.NodeName = xmlnodeList[0]) then
begin
for i := 1 to urlcount-1 do //扫描节点
begin
if(XmlNode <> nil) then
XmlNode := getnodefromIXMLNodeList(XmlNode.ChildNodes,xmlnodeList)
else Break ;
end;
end;


except
// MessageBox()
end;
finally
// xmlDocument.Active:=false;
end;
Result := (XmlNode<> nil) and (i=urlcount) ;


xmlnodeList.free;

end;

procedure TNormal.Xmlsave;
begin
XmlDocument.SaveToFile(FXmlFileName);
end;

end.

程序运行到 : XmlNode := xmlDocument.DocumentElement; 就报读地址错了。
???????
 
xmlnodeList.DelimitedText := NodePath ; //
这里不一样了,原句是xmlnodeList.DelimitedText:='entity-config.RecordFile.SysRecord';
估计是Active :=true时打开错误
 
注意你的对象和接口不要混用,这里的 FXmlDocument: TXMLDocument; 改为 FXmlDocument: IXMLDocument; 其他的不变
 
唉。又范低级错误了。LSUPER,此分非你莫属了。明天再结帐吧
 

Similar threads

I
回复
0
查看
745
import
I
I
回复
0
查看
729
import
I
I
回复
0
查看
729
import
I
I
回复
0
查看
507
import
I
后退
顶部