delphi 读取xml
加载xml文件的出现错误,我读取已经写好的xml就会提示错误,错误如下 //我加载xml代码如下procedureTForm1.FormCreate(Sende...
加载xml文件的出现错误,我读取已经写好的xml就会提示错误,错误如下 //我加载xml代码如下procedure TForm1.FormCreate(Sender: TObject);begin FXMLDoc := TXMLDocument.Create(nil); FXMLDoc.XML.Text := 'D:\1.xml';// 执行这个是时候FXMLDoc.XML.Text值为:D:\1.xml #$D#$A FXMLDoc.Active := True; FList := TList.Create; FDocElement := FXMLDoc.DocumentElement;end; //我的xml里面的写的是如下<?xml version="1.0" encoding="GB2312"?> <Students> <Student Sex = "男"> <Name>文文</Name> <Age>18</Age> <Phone>4745723</Phone> </Student> <Student Sex = "女"> <Name>喃喃</Name> <Age>17</Age> <Phone>4644563</Phone> </Student> </Students> 哪位朋友告诉我下。 谢谢
展开
1个回答
展开全部
XML文档内容如下:
-------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="GB2312"?>
<ERP>
<Job SessionID="ERTGF54435GDFG345234F" ActionName="ActionNewJob_New" Job="New_Job" ServletDir="ServletDir_List"/>
<Main ID="2011" MenuName="基础管理" MenuCode="主表" CallName="CallInMain" DllName="InMain.DLL"/>
</ERP>
-------------------------------------------------------------------------------------------------------
使用TNativeXML解析方法如下:
procedure ReadXML(CDSMainTemp, CDSMain: TClientDataSet);
var
XMLDoc: TNativeXML;
RootNode, MainNode, LineNode, LineNode1, SubLineNode: TXMLNode;
kkk, kk, mingxi1, mingxi2, MainNum, LineNum, Line1Num, SubLineNum, RootNum, RowNum, k, i, j: integer;
job, ss, S: string;
begin
XMLDoc := TNativeXML.Create;
XMLDoc.LoadFromFile('C:\Documents and Settings\Test\桌面\ERP 1.0\ERP.xml');
RootNode := XMLDoc.Root; //ERP
MainNode := RootNode.Nodes[1]; //Main
LineNode := MainNode.Nodes[1]; //Line
MainNum := RootNode.NodeCount; //主表的行数
RowNum := MainNode.AttributeCount; //主表的字段数目
LineNum := MainNode.NodeCount; //主表的明细行数
// job := RootNode.Nodes[0].Name;
/// ShowMessage(job);
CDSMainTemp.EmptyDataSet;
CDSMain.EmptyDataSet;
for k := 0 to MainNum - 1 do //ERP节点下面的第二层主表行数循环
begin
if RootNode.Nodes[k].Name = 'Job' then
begin
RowNum := RootNode.Nodes[0].AttributeCount;
CDSMainTemp.Append;
for j := 0 to RowNum - 1 do //主表字段数目循环
begin //读取XML文档中一行节点名和节点值并赋值到数据集
for i := 0 to CDSMainTemp.Fields.Count - 1 do //循环匹配数据集字段赋值
begin
CDSMainTemp.FieldByName(RootNode.Nodes[0].AttributeName[i]).AsString := RootNode.Nodes[0].AttributeValue[i]; //MainNode.Nodes[j].AttributeValue[j];
end;
end;
CDSMainTemp.Post;
end
else if RootNode.Nodes[k].Name = 'Main' then
begin
RowNum := RootNode.Nodes[1].AttributeCount;
CDSMain.Append;
for j := 0 to RowNum - 1 do //主表字段数目循环
begin //读取XML文档中一行节点名和节点值并赋值到数据集
for i := 0 to CDSMain.Fields.Count - 1 do //循环匹配数据集字段赋值
begin
CDSMain.FieldByName(RootNode.Nodes[k].AttributeName[j]).AsString := RootNode.Nodes[k].AttributeValue[j]; //MainNode.Nodes[j].AttributeValue[j];
end;
end;
CDSMain.Post;
end;
end;
end;
-------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="GB2312"?>
<ERP>
<Job SessionID="ERTGF54435GDFG345234F" ActionName="ActionNewJob_New" Job="New_Job" ServletDir="ServletDir_List"/>
<Main ID="2011" MenuName="基础管理" MenuCode="主表" CallName="CallInMain" DllName="InMain.DLL"/>
</ERP>
-------------------------------------------------------------------------------------------------------
使用TNativeXML解析方法如下:
procedure ReadXML(CDSMainTemp, CDSMain: TClientDataSet);
var
XMLDoc: TNativeXML;
RootNode, MainNode, LineNode, LineNode1, SubLineNode: TXMLNode;
kkk, kk, mingxi1, mingxi2, MainNum, LineNum, Line1Num, SubLineNum, RootNum, RowNum, k, i, j: integer;
job, ss, S: string;
begin
XMLDoc := TNativeXML.Create;
XMLDoc.LoadFromFile('C:\Documents and Settings\Test\桌面\ERP 1.0\ERP.xml');
RootNode := XMLDoc.Root; //ERP
MainNode := RootNode.Nodes[1]; //Main
LineNode := MainNode.Nodes[1]; //Line
MainNum := RootNode.NodeCount; //主表的行数
RowNum := MainNode.AttributeCount; //主表的字段数目
LineNum := MainNode.NodeCount; //主表的明细行数
// job := RootNode.Nodes[0].Name;
/// ShowMessage(job);
CDSMainTemp.EmptyDataSet;
CDSMain.EmptyDataSet;
for k := 0 to MainNum - 1 do //ERP节点下面的第二层主表行数循环
begin
if RootNode.Nodes[k].Name = 'Job' then
begin
RowNum := RootNode.Nodes[0].AttributeCount;
CDSMainTemp.Append;
for j := 0 to RowNum - 1 do //主表字段数目循环
begin //读取XML文档中一行节点名和节点值并赋值到数据集
for i := 0 to CDSMainTemp.Fields.Count - 1 do //循环匹配数据集字段赋值
begin
CDSMainTemp.FieldByName(RootNode.Nodes[0].AttributeName[i]).AsString := RootNode.Nodes[0].AttributeValue[i]; //MainNode.Nodes[j].AttributeValue[j];
end;
end;
CDSMainTemp.Post;
end
else if RootNode.Nodes[k].Name = 'Main' then
begin
RowNum := RootNode.Nodes[1].AttributeCount;
CDSMain.Append;
for j := 0 to RowNum - 1 do //主表字段数目循环
begin //读取XML文档中一行节点名和节点值并赋值到数据集
for i := 0 to CDSMain.Fields.Count - 1 do //循环匹配数据集字段赋值
begin
CDSMain.FieldByName(RootNode.Nodes[k].AttributeName[j]).AsString := RootNode.Nodes[k].AttributeValue[j]; //MainNode.Nodes[j].AttributeValue[j];
end;
end;
CDSMain.Post;
end;
end;
end;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询