TXMLDocument를 이용하여 XML파일을 생성하고 읽어오는 예제이며, 에러처리등의 부분은
빠져있을 수 있음.
uses XMLDoc, XMLIntf...
- XML File Create
Var
XMLDoc : TXMLDocument;RootNode : IXMLNode;
ChildNode : IXMLNode;
begin
XMLDoc := TXMLDocument.Create(Application);
XMLDoc.Active := True;RootNode := XMLDoc.AddChild('CreateXML Test');
RootNode.Attributes['app'] := ParamStr(0);XMLDoc.Encoding:= 'euc-kr';
ChildNode := RootNode.AddChild('item');
ChildNode.Attributes['text'] := 'text';
ChildNode.Attributes['kind'] := 'kind';
ChildNode.Attributes['url'] := 'url';XMLDoc.SaveToFile('C:\Test.Xml');
XMLDoc.Free;
- XML File Read
Var
XMLDoc : TXMLDocument;
ChildNode : IXMLNode;I, J : Integer;
begin
XMLDoc := TXMLDocument.Create(Application);
XMLDoc.LoadFromFile('C:\Test.Xml');
XMLDoc.Active := True;if XMLDoc.ChildNodes.First = nil then Begin
Exit;
End;
For I := 0 To XMLDoc.DocumentElement.ChildNodes.count - 1 Do Begin
ChildNode := XMLDoc.DocumentElement.ChildNodes[i];
For J := 0 To ChildNode.AttributeNodes.Count - 1 Do Begin
ShowMessage(
ChildNode.AttributeNodes.Get(J).NodeName + #13 +
ChildNode.AttributeNodes.Get(J).NodeValue );
// ChildNode.AttributeNodes.Get(J).NodeType + #13 +
End;
End;
XMLDoc.Free;
End;