TXMLDocument对XML文件进行读取和写入 (bcb)

动态创建TXMLDocument对XML文件进行读取和写入 - [Delphi高级应用]

2008-01-16

版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
http://overblue.blogbus.com/logs/13954596.html

uses
  XMLDoc, XMLIntf;

{ 写入XML内容 }
var
  XMLDoc : TXMLDocument;
  Node1 : IXMLNode;
  Node2 : IXMLNode;
begin
  XMLDoc := TXMLDocument.Create(nil);
  try
    XMLDoc.Active := True;
    XMLDoc.Version := '1.0';
    XMLDoc.Encoding := 'GB2312';
    XMLDoc.Options := [doNodeAutoCreate,doNodeAutoIndent,doAttrNull,doAutoPrefix,doNamespaceDecl];

    XMLDoc.DocumentElement := XMLDoc.CreateNode('ReportObjectContent');
    Node1 := XMLDoc.DocumentElement;

    Node1 := Node1.AddChild('ReportObjectProperty');
    Node2 := Node1.AddChild('ReportName');
    Node2.SetAttributeNS('Value', '', ReportName);
    Node2 := Node1.AddChild('ReportType');
    Node2.SetAttributeNS('Value', '', ReportType);
    Node2 := Node1.AddChild('DataViewName');
    Node2.SetAttributeNS('Value', '', DataViewName);
    Node2 := Node1.AddChild('SQLStr');
    Node2.SetAttributeNS('Value', '', SQLStr);   

    XMLDoc.SaveToStream(Stream);
  finally
    XMLDoc.Free;
  end;
end;


{ 读取XML }
var
  XML : TXMLDocument;
  Node1 : IXMLNode;
  DocIntf : IXMLDocument;
begin
  XML := TXMLDocument.Create(nil);
  DocIntf := XML;  //防止接口被自动释放,少了这一句会发生AV
  try
    XML.LoadFromStream(Strem);
    XML.Active := True;

    { 读ReportObject属性 }
    Node1 := XML.DocumentElement.ChildNodes.FindNode('ReportObjectProperty');
    ReportName := Node1.ChildNodes.FindNode('ReportName').GetAttributeNS('Value', '');
    ReportType := Node1.ChildNodes.FindNode('ReportType').GetAttributeNS('Value', '');
    DataViewName := Node1.ChildNodes.FindNode('DataViewName').GetAttributeNS('Value', '');
    SQLStr := Node1.ChildNodes.FindNode('SQLStr').GetAttributeNS('Value', '');

  finally
    XML := nil;
    DocIntf := nil;
  end;
end;

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值