Have a look at Delphi's own TXMLDocument
component, for example:
uses
..., XMLIntf, XMLDoc;
procedure TForm1.FormCreate(Sender: TObject);
var
Vehicle: IXMLNode;
begin
XMLDocument1.FileName :='vehicle.xml';
XMLDocument1.Active := True;
try
Vehicle := XMLDocument.DocumentElement;
txtType.Text := Vehicle.ChildNodes['type'].Text;
txtModel.Text := Vehicle.ChildNodes['model'].Text;
txtnumber.Text := Vehicle.ChildNodes['number'].Text;
finally
XMLDocument1.Active := False;
end;
end;
Alternatively, use the IXMLDocument
interface directly (which TXMLDocument
implements):
uses
..., XMLIntf, XMLDoc;
procedure TForm1.FormCreate(Sender: TObject);
var
Doc: IXMLDocument;
Vehicle: IXMLNode;
begin
Doc := LoadXMLDocument('vehicle.xml');
Vehicle := Doc.DocumentElement;
txtType.Text := Vehicle.ChildNodes['type'].Text;
txtModel.Text := Vehicle.ChildNodes['model'].Text;
txtnumber.Text := Vehicle.ChildNodes['number'].Text;
end;
Update: the XML in the question has been altered to now wrap the vehicle
element inside of a data
element, and to have multiple vehicle
elements. So the code above has to be adjusted accordingly, eg:
uses
..., XMLIntf, XMLDoc;
procedure TForm1.FormCreate(Sender: TObject);
var
Doc: IXMLDocument;
Data: IXMLNode;
Node: IXMLNode;
I: Integer;
begin
Doc := LoadXMLDocument('vehicle.xml');
Data := Doc.DocumentElement;
for I := 0 to Data.ChildNodes.Count-1 do
begin
Node := Data.ChildNodes[I];
// if all of the child nodes will always be 'vehicle' only
// then this check can be removed...
if Node.LocalName = 'vehicle' then
begin
// use Node.ChildNodes['type'], Node.ChildNodes['model'],
// and Node.ChildNodes['number'] as needed...
end;
end;
end;