• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

TXMLDocumentusecase(Delphi)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

This example illustrates the basic operations on an XML document.

Code

procedure CreateDocument;
const
  CFilename = 'file.xml';
var
  LDocument: IXMLDocument;
  LNodeElement, NodeCData, NodeText: IXMLNode;
begin
  LDocument := TXMLDocument.Create(nil);
  LDocument.Active := True;
 
  { Define document content. }
  LDocument.DocumentElement := LDocument.CreateNode('ThisIsTheDocumentElement', ntElement, '');
  LDocument.DocumentElement.Attributes['attrName'] := 'attrValue';
  LNodeElement := LDocument.DocumentElement.AddChild('ThisElementHasText', -1);
  LNodeElement.Text := 'Inner text.';
  NodeCData := LDocument.CreateNode('any characters here', ntCData, '');
  LDocument.DocumentElement.ChildNodes.Add(NodeCData);
  NodeText := LDocument.CreateNode('This is a text node.', ntText, '');
  LDocument.DocumentElement.ChildNodes.Add(NodeText);
 
  LDocument.SaveToFile(CFilename);
end;
 
procedure RetrieveDocument;
const
  CFilename = 'file.xml';
  CAttrName = 'attrName';
  HTAB = #9;
var
  LDocument: IXMLDocument;
  LNodeElement, LNode: IXMLNode;
  LAttrValue: string;
  I: Integer;
begin
  LDocument := TXMLDocument.Create(nil);
  LDocument.LoadFromFile(CFilename);
 
  { Find a specific node }
  LNodeElement := LDocument.ChildNodes.FindNode('ThisIsTheDocumentElement');
 
  if (LNodeElement <> nil) then
  begin
    { Get a specific attribute }
    Writeln('Getting attribute...');
    if (LNodeElement.HasAttribute(CAttrName)) then
    begin
      LAttrValue := LNodeElement.Attributes[CAttrName];
      Writeln('Attribute value: ' + LAttrValue);
    end;
 
    { Traverse child nodes }
    Writeln(sLineBreak, 'Traversing child nodes...' + sLineBreak);
    for I := 0 to LNodeElement.ChildNodes.Count - 1 do
    begin
      LNode := LNodeElement.ChildNodes.Get(I);
      { Display node name }
      Writeln(sLineBreak + 'Node name: ' + LNode.NodeName);
      { Check if the node type is Text. }
      if LNode.NodeType = ntText then
      begin
        Writeln(HTAB + 'This is a node of type Text. The text is: ' + LNode.Text);
      end;
      { Check if the node is text element. }
      if LNode.IsTextElement then
      begin
        Writeln(HTAB + 'This is a text element. The text is: ' + LNode.Text);
      end;
    end;
  end;
end;

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
谈Delphi编程中“流”的应用发布时间:2022-07-18
下一篇:
delphi中VirtualStringTree树使用方法之终结篇!发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap