zoukankan      html  css  js  c++  java
  • TXMLDocument use case (Delphi)

    Description

    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;
    
  • 相关阅读:
    c++智能指针-shared_ptr
    python全栈学习笔记(二)网络基础之子网划分
    python全栈学习笔记(一)网络基础之网络协议篇
    Fiddler抓包4-工具介绍(request和response)
    python接口自动化5-Json数据处理
    python接口自动化4-绕过验证码登录(cookie) (转载)
    Http status(二)
    python接口自动化1-发送get请求
    Fiddler抓包11-HTTPS证书Actions无法导出问题
    使用idea配置tomcat将web项目跑起来
  • 原文地址:https://www.cnblogs.com/MaxWoods/p/3179246.html
Copyright © 2011-2022 走看看