Question/Problem/Abstract:
Here you can found easy examples of how to read an XML file using a TXMLDocument control, nothing more, nothing less.
Answer:
Here you can find different examples of how to read an XML document using the TXMLDocument control. For more information about the TXMLDocument control, you can see the article 2714 written by S S B Magesh Puvananthiran: http://www.delphi3000.com/articles/article_2714.asp
All this examples are used with permission of Korpos Studios: http://www.korpos.com.ar
//---------------------------------------------------------------------------
First example:
Search XML nodes by name and put the text of each node in a TListBox.
//---------------------------------------------------------------------------
procedure TMain.FormCreate(Sender: TObject);
begin
with (XMLDocument) do begin
FileName := 'Example.xml'; // Sets XML file
Active := true; // Activates control
// Here we use the DocumentElement propertie wich gives us access to
// the root node of the XML file
with (DocumentElement.ChildNodes) do begin // Search nodes by name
ListBox.AddItem(FindNode('node1').Text, nil); // Adds node1's text to list
ListBox.AddItem(FindNode('node2').Text, nil); // Adds node2's text to list
ListBox.AddItem(FindNode('node4').Text, nil); // Adds node4's text to list
end;
end;
end;
//---------------------------------------------------------------------------
Source code of this example: http://www.planetgamer.com.ar/delphi3000/XMLexample01.zip
//---------------------------------------------------------------------------
Second example:
Same as above, but now we get a node first and then we get its subnodes and displays the text of each subnode.
//---------------------------------------------------------------------------
procedure TMain.FormCreate(Sender: TObject);
var tempNode : IXMLNode;
begin
with (XMLDocument) do begin
FileName := 'Example.xml'; // Sets XML file
Active := true; // Activates control
// Here we use the DocumentElement propertie wich gives us access to
// the root node of the XML file
tempNode := DocumentElement.ChildNodes.FindNode('node3'); // Gets node3
ListBox.AddItem(tempNode.ChildNodes.FindNode('subnode1').Text, nil); // Adds subnode1 text to list
ListBox.AddItem(tempNode.ChildNodes.FindNode('subnode2').Text, nil); // Adds subnode1 text to list
ListBox.AddItem(tempNode.ChildNodes.FindNode('subnode3').Text, nil); // Adds subnode1 text to list
end;
end;
//---------------------------------------------------------------------------
Source code of this example: http://www.planetgamer.com.ar/delphi3000/XMLexample02.zip
//---------------------------------------------------------------------------
Third example:
Now we don't know the names of the nodes, so we get all the nodes in the XML document.
//---------------------------------------------------------------------------
procedure TMain.FormCreate(Sender: TObject);
var Cont : byte;
begin
with (XMLDocument) do begin
FileName := 'Example.xml'; // Sets XML file
Active := true; // Activates control
// Here we use the DocumentElement propertie wich gives us access to
// the root node of the XML file
for Cont := 0 to DocumentElement.ChildNodes.Count - 1 do
ListBox.AddItem(DocumentElement.ChildNodes.Get(Cont).NodeName, nil); // Adds the name of nodes to list
end;
end;
//---------------------------------------------------------------------------
Source code of this example: http://www.planetgamer.com.ar/delphi3000/XMLexample03.zip
//---------------------------------------------------------------------------
Fourth example:
Same as above but now we get all the subnodes also (if any).
Note1: The text in childless nodes appears as a children node, for identify real child nodes we can use a property called IsTextElement.
Note2: This example is not recursive, so we get only 2 levels of nodes/childs.
//---------------------------------------------------------------------------
procedure TMain.FormCreate(Sender: TObject);
var tempNode : IXMLNode; Cont, Cont2 : byte;
begin
with (XMLDocument) do begin
FileName := 'Example.xml'; // Sets XML file
Active := true; // Activates control
// Here we use the DocumentElement propertie wich gives us access to
// the root node of the XML file
for Cont := 0 to DocumentElement.ChildNodes.Count - 1 do begin
tempNode := DocumentElement.ChildNodes.Get(Cont);
ListBox.AddItem(tempNode.NodeName, nil); // Adds node's name to list
if (tempNode.HasChildNodes) then // Has childs ?
for Cont2 := 0 to tempNode.ChildNodes.Count - 1 do // Gets child nodes
// Here we've got a problem, in a childless node the its text is taken
// as a child, so we need to validate if its child is a text element,
// so we can filter it and adds only real child nodes
if (tempNode.ChildNodes.Get(Cont2).IsTextElement) then // Is a text or a child ?
ListBox.AddItem(tempNode.ChildNodes.Get(Cont2).NodeName, nil); // Adds child node's name to list
end;
end;
end;
//---------------------------------------------------------------------------
Source code of this example: http://www.planetgamer.com.ar/delphi3000/XMLexample04.zip
//---------------------------------------------------------------------------
Fifth example:
Similar to previous examples, but now we get nodes' attributes (id attribute in this particular example).
//---------------------------------------------------------------------------
procedure TMain.FormCreate(Sender: TObject);
var tempNode : IXMLNode; Cont : byte;
begin
with (XMLDocument) do begin
FileName := 'Example.xml'; // Sets XML file
Active := true; // Activates control
// Here we use the DocumentElement propertie wich gives us access to
// the root node of the XML file
for Cont := 0 to DocumentElement.ChildNodes.Count - 1 do begin
tempNode := DocumentElement.ChildNodes.Get(Cont);
if (tempNode.HasAttribute('id')) then // Has id attribute ?
ListBox.AddItem(tempNode.Attributes['id'], nil); // Adds node's attribute to list
end;
end;
end;
//---------------------------------------------------------------------------
Source code of this example: http://www.planetgamer.com.ar/delphi3000/XMLexample05.zip
//---------------------------------------------------------------------------
More examples on the way. I'm planning to add recursive examples, combinations with trees, write XML examples, etc.