建立和解析XML

來源:互聯網
上載者:User

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, MSXML_TLB,Comobj, ComCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    tvXML: TTreeView;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure tvXMLClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
//建立
procedure TForm1.Button1Click(Sender: TObject);
var
  doc : IXMLDOMDocument;
  root,{child1,child2,}childtmp : IXMLDomElement;
  //XMLAtri : IXMLDOMAttribute;
  procedure addSysNode(pElement : IXMLDomElement;iDepth : Integer);
  var
    child1 : IXMLDomElement;
    i      : Integer;
  begin
    child1 := doc.createElement('SysNode'+inttostr(iDepth));
    child1.setAttribute('Vol','102');
    child1.setAttribute('Option','2');
    child1.setAttribute('ConnectID','1');
    child1.setAttribute('No','0');
    child1.setAttribute('Num','-1');
    pElement.appendChild(child1);

    if iDepth = 5 then
    begin
      exit;
    end;

    addSysNode(child1,iDepth+1);

  end;
begin
  doc := CreateOLEObject('Microsoft.XMLDOM') as IXMLDomDocument;
  root := doc.CreateElement('Site');
  root.setAttribute('type','7');
  doc.AppendChild(root);
 
  childtmp := doc.CreateElement('SystemNode1');
  childtmp.setAttribute('Option','2');
  childtmp.setAttribute('ConnectID','1');
  childtmp.setAttribute('No','0');
  childtmp.setAttribute('Num','63');
  root.AppendChild(childtmp);

  childtmp := doc.CreateElement('SystemNode2');
  childtmp.setAttribute('Option','2');
  childtmp.setAttribute('ConnectID','1');
  childtmp.setAttribute('No','0');
  childtmp.setAttribute('Num','63');
  root.AppendChild(childtmp);

  childtmp := doc.createElement('SystemNode');
  childtmp.setAttribute('Vol','102');
  childtmp.setAttribute('Option','2');
  childtmp.setAttribute('ConnectID','1');
  childtmp.setAttribute('No','0');
  childtmp.setAttribute('Num','-1');
  root.AppendChild(childtmp);

  addSysNode(childtmp,0);
 
  doc.save('e:/xmltest.xml');
end;

//解析
procedure TForm1.Button2Click(Sender: TObject);
var
  //i : integer;
  doc  : IXMLDOMDocument;
  root : IXMLDOMNode;
  tvRoot,curtvNode : TTreeNode;
  TreeNodeList : TList;
  atriArray : array[1..6] of String; //屬性列表數組
  {
    **根據傳入的屬性名稱擷取屬性值
  }
  function getAtri(pNode : IXMLDOMNODE; atriName,atrOption : String):string;
  var
    tmpAtri1 : IXMLDOMNode;
  begin
    tmpAtri1 := pNode.attributes.getNamedItem(atriName);
    if tmpAtri1 <> nil then
    begin
      if atrOption = '' then
      begin
        atrOption := tmpAtri1.nodeName+'='+tmpAtri1.nodeValue;
      end
      else
      begin
        atrOption := atrOption+','+tmpAtri1.nodeName+'='+tmpAtri1.nodeValue;
      end;
    end;
    result := atrOption;
  end;

  {
   **遞迴擷取層次節點
  }
  procedure addNode(pNode : IXMLDOMNode;ptvNode : TTreeNode);
  var
    i       : Integer;
    tmpRoot,childtmp : IXMLDOMNode;
    bBrother : Boolean;
    atrOption : String;
  begin
    bBrother := False;
    while(pNode <> nil) do
    begin
      atrOption := '';

      for i := 1 to 6 do
      begin
        atrOption := getAtri(pNode,atriArray[i],atrOption);
      end;
     
      if not bBrother then
      begin
        ptvNode := tvXML.Items.AddChild(ptvNode,pNode.nodeName+'('+atrOption+')'); //父子節點
      end
      else
      begin
        ptvNode := tvXML.Items.Add(ptvNode,pNode.nodeName+'('+atrOption+')');   //兄弟節點
      end;
      if pNode.childNodes.length > 0 then
      begin
        childtmp := pNode.childNodes.item[0];
        addNode(childtmp,ptvNode);
        //ptvNode := TreeNodeList.
      end;
      pNode := pNode.nextSibling;
      bBrother := True;
    end;
  end;
begin
  doc := CreateOleObject('Microsoft.XMLDOM') as IXMLDomDocument;
  doc.load('E:/xmltest.xml');

  //屬性名稱
  atriArray[1] := 'type';
  atriArray[2] := 'Vol';
  atriArray[3] := 'Option';
  atriArray[4] := 'ConnectID';
  atriArray[5] := 'No';
  atriArray[6] := 'Num';
 
  root := doc.firstChild;
  addNode(root,nil);

end;

procedure TForm1.tvXMLClick(Sender: TObject);
var
  tvNode : TTreeNode;
begin
  tvNode := tvXML.Selected;
 
  if tvNode.Text = '2' then
     exit;
end;

end.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.