手头上有个比较复杂的XML文档。格式如下:
<?xml version="1.0" encoding="GB2312"?>
<ASDF>
<Attribute ServerIP="" IsStart="false"/>
<DN>
<Res Desc="Tools1">
<DNList>
<ComplexDN StartDN="0001" EndDN="0001"/>
</DNList>
<RunTime>
<RunPeriod Begin="0" End="1">
<Application Name="ASD" XMLFile="a.exe" Isbound="true" IsAutoStart="false"/>
</RunPeriod>
</RunTime>
</Res>

        
</DN>
</ASDF>请问这该如何去获得相应的XML的值?比如我要获得其中 StartDN 和 XMLFile 的值,应该如何去写代码呢?

解决方案 »

  1.   

    有个XML文件生成向导的,可以试试...
      

  2.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, xmldom, XMLIntf, StdCtrls, msxmldom, XMLDoc;type
      TForm1 = class(TForm)
        mXml: TXMLDocument;
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    var
            mStr: string;
            i,j: integer;
            mRootNode,mChildNode:IXMLNode;
    begin
            try
                    mXml.LoadFromFile('d:\test.xml');
                    mXml.Active := True;
                    mXml.version := '1.0';
                    mXml.encoding :='gb2312';
            except
                    on E: Exception do
                    begin
                            ShowMessage('XML导入失败:' + E.Message + ':' + IntToStr(E.HelpContext));
                            exit;
                    end;
            end;
            mRootNode := mXml.DocumentElement;
            mChildNode := mRootNode.ChildNodes.FindNode('DN');
            mChildNode := mChildNode.ChildNodes.FindNode('Res');
            mChildNode := mChildNode.ChildNodes.FindNode('DNList');
            mChildNode := mChildNode.ChildNodes.FindNode('ComplexDN');
            mStr := mChildNode.AttributeNodes['StartDN'].NodeValue;
            ShowMessage('StartDN='+mStr);
            mChildNode := mRootNode.ChildNodes.FindNode('DN');
            mChildNode := mChildNode.ChildNodes.FindNode('Res');
            mChildNode := mChildNode.ChildNodes.FindNode('RunTime');
            mChildNode := mChildNode.ChildNodes.FindNode('RunPeriod');
            mChildNode := mChildNode.ChildNodes.FindNode('Application');
            mStr := mChildNode.AttributeNodes['XMLFile'].NodeValue;
            ShowMessage('XMLFile='+mStr);
    end;end.
      

  3.   

    TXMLDocument的组件,在delphi的internet下面