2011-12-28 79 views
1

我想寫一些代碼(在Delphi中)來獲得這個XML方案,我試過但沒有結果,因爲我想,你能幫助我! 我使用(或希望使用)在運行時創建IXMLDocument,但我無法理解「節點」,「ChildNodes」......我知道,這太可笑了!如何用Delphi創建這個XML?

這是該計劃的例子,我想:

<Items> 
<Task id="eec0-47de-91bc-98e2d69d75cd"> 
    <Title>The title of something</Title> 
    <State>Done</State> 
    <IdNoHashed>This Is a string</IdNoHashed> 
    <CreatedDate>28/12/2011 06:24:57</CreatedDate> 
    <Note>Just a note</Note> 
</Task> 
<Task id="e2x5d4-2d45c-98e2d69d75cd"> 
    <Title>Another title</Title> 
    <State>Done</State> 
    <IdNoHashed>This Is a string 2</IdNoHashed> 
    <CreatedDate>28/12/2011 22:22:22</CreatedDate> 
    <Note>Just a note, again !</Note> 
</Task> 
</items> 

你有一個建議嗎? 謝謝!

編輯:我試了下面的代碼回答,它工作正常,但是當我想在根中添加任何其他條目時,它會重寫已存在的元素。

Function WriteData (id, title, state, idNH : String) : Boolean; 
var 
    Doc: IXMLDocument; 
    Items, Task: IXMLNode; 
begin 
    Doc := NewXMLDocument; 
    Items := Doc.AddChild('Items'); 

    Task := Items.AddChild('Task'); 
    Task.Attributes['id'] := id; 
    Task.AddChild('Title').Text := title; 
    Task.AddChild('State').Text := state; 
    Task.AddChild('IdNoHashed').Text := idNH; 
    Task.AddChild('CreatedDate').Text := DateTimeToStr(Now); 
    Task.AddChild('Note').Text := 'Just a note'; 
end; 

我試過DocumentElement.ChildNodes.FindNode(id),但沒有成功!

我創建了一個函數,我每次調用添加/修改XML文件中的條目時,條目都是「」。 一個想法,我該如何做到這一點?! 謝謝!

回答

6

檢查哪個使用IXMLDocument接口此示例應用程序,在源的註釋解釋了元素是如何加入

{$APPTYPE CONSOLE} 

{$R *.res} 

uses 
    ActiveX, 
    XMLIntf, 
    XMLDoc, 
    SysUtils; 

procedure Test; 
Var 
    XML : IXMLDocument; 
    RootNode, Node, CNode : IXMLNode; 
begin 
    XML := NewXMLDocument;//initializate the interface 
    XML.Options := [doNodeAutoIndent];//activate the auto indentation 
    RootNode := XML.AddChild('Items');//add the root node 
    Node := RootNode.AddChild('Task');//add the task node 
    Node.Attributes['id'] := 'eec0-47de-91bc-98e2d69d75cd';//<Task id="eec0-47de-91bc-98e2d69d75cd"> 
    CNode:=Node.AddChild('Title');//add the title node 
    CNode.Text:='The title of something';//<Title>The title of something</Title> 
    CNode:=Node.AddChild('State');//add the State node 
    CNode.Text:='Done'; //<State>Done</State> 
    CNode:=Node.AddChild('IdNoHashed');//add the IdNoHashed node 
    CNode.Text:='This Is a string'; //<IdNoHashed>This Is a string</IdNoHashed> 
    CNode:=Node.AddChild('CreatedDate');//add the CreatedDate node 
    CNode.Text:=FormatDateTime('dd/mm/yyyy hh:nn:ss',EncodeDate(2011,12,28)+EncodeTime(6,24,57,0));//<CreatedDate>28/12/2011 06:24:57</CreatedDate> 
    CNode:=Node.AddChild('Note');//Add the Note node 
    CNode.Text:='Just a note'; //<Note>Just a note</Note> 

    //repeat the process again for the second task node  
    Node := RootNode.AddChild('Task'); 
    Node.Attributes['id'] := 'e2x5d4-2d45c-98e2d69d75cd'; 
    CNode:=Node.AddChild('Title'); 
    CNode.Text:='Another title'; 
    CNode:=Node.AddChild('State'); 
    CNode.Text:='Done'; 
    CNode:=Node.AddChild('IdNoHashed'); 
    CNode.Text:='This Is a string 2'; 
    CNode:=Node.AddChild('CreatedDate'); 
    CNode.Text:=FormatDateTime('dd/mm/yyyy hh:nn:ss',EncodeDate(2011,12,28)+EncodeTime(22,22,22,0)); 
    CNode:=Node.AddChild('Note'); 
    CNode.Text:='Just a note, again !'; 

    Writeln(XML.XML.Text); //Show the output 
end; 


begin 
try 
    CoInitialize(nil); //use this just in console apps 
    try 
     Test; 
    finally 
     CoUninitialize;//use this just in console apps 
    end; 
except 
    on E:Exception do 
     Writeln(E.Classname, ':', E.Message); 
end; 
Writeln('Press Enter to exit'); 
Readln; 
end. 
+0

非常感謝你,爲了準確性,併爲評論,我終於明白了事情!謝謝 !! – djiga4me 2011-12-28 17:06:02

5

像這樣:

var 
    Doc: IXMLDocument; 
    Items, Task: IXMLNode; 
begin 
    Doc := NewXMLDocument; 
    Items := Doc.AddChild('Items'); 

    Task := Items.AddChild('Task'); 
    Task.Attributes['id'] := 'eec0-47de-91bc-98e2d69d75cd'; 
    Task.AddChild('Title').Text := 'The title of something'; 
    Task.AddChild('State').Text := 'Done'; 
    Task.AddChild('IdNoHashed').Text := 'This Is a string'; 
    Task.AddChild('CreatedDate').Text := DateTimeToStr(Now); 
    Task.AddChild('Note').Text := 'Just a note'; 

    Task := Items.AddChild('Task'); 
    Task.Attributes['id'] := 'e2x5d4-2d45c-98e2d69d75cd'; 
    Task.AddChild('Title').Text := 'Another title'; 
    Task.AddChild('State').Text := 'Done'; 
    Task.AddChild('IdNoHashed').Text := 'This Is a string 2'; 
    Task.AddChild('CreatedDate').Text := DateTimeToStr(Now); 
    Task.AddChild('Note').Text := 'Just a note, again!'; 

// save Doc as needed... 
// Doc.SaveToStream(...); 
// Doc.SaveToFile(...); 
// Doc.SaveToXML(...); 
// XML := Doc.XML.Text; 
// etc... 

端;

+0

謝謝,這很簡單,(只需添加爲Doc.Options:= [doNodeAutoIndent])縮進事物!我會用這個方便! – djiga4me 2011-12-28 17:08:11