实现步骤:
1)建立新项目和窗体,命名为LoadXML.dpr和frmMain;
2)注意包括以下几个必须的包:
SysUtils, xmldom, XMLIntf, msxmldom, XMLDoc, Variants;
3)建立以下主要控件:
tvXML: TTreeView; // Treeview representation the XML's tree structure
dlgOPen: TOpenDialog;
btnOpen: TButton;
imgMain: TImageList; // Stored the icon used on Treeview
XMLDoc: TXMLDocument;
chkExpTree: TCheckBox; // decide whether expand the tree node
chkDispText: TCheckBox;// decide whether display the value of xml node
rEdtNodeValue: TRichEdit;
4)建立以下私有变量:
myXMLNode: IXMLNode;
xmlRootNode: IXMLNode;
myRootNode: TTreeNode;
定义几个常量:
SYS_NAME = 'XML Data Manager System Version1.0';
ii)XML文件打开后,进行解析其树状结构!
procedure TfrmMain.XMLDocAfterOpen(Sender: TObject);
begin
var
dtS, dtE: TDateTime;
begin
dtS := Now();
// Get the root node of xml document!
xmlRootNode := XMLDoc.DocumentElement;
// Clear all items of TreeView and begin to add many items
frmMain.tvXML.Items.Clear;
frmMain.tvXML.Items.BeginUpdate;
myRootNode := frmMain.tvXML.Items.Add(nil, xmlRootNode.NodeName);
if frmMain.chkDispText.Checked then
CheckXMLNode(myRootNode, xmlRootNode, True)
else
CheckXMLNode(myRootNode, xmlRootNode, False);
frmMain.tvXML.Items.EndUpdate;
// Now the added items operation finished!
if frmMain.chkExpTree.Checked then
myRootNode.Expand(False);
其中使用的函数CheckXMLNode()如下:
// --------------------------------------------------------- //
// Function Name: CheckXMLNode(myTreeNode:TTreeNode; myXMLNode: IXMLNode; bolDisplayText: Boolean): Boolean
// Description: Check the XML node which specified and get his own attributes and child's node information;
// Input Parameter: 1) myTreeNode: TTreeNode -- Current node of TreeView control
// 2) myXMLNode: IXMLNode -- Current node of XML document
// 3) bolDisplayText: Boolean -- Switch determined whether display the content of current XML node
// Output Parameter: None;
// Comment: 1) Current node is a leaf, then return and display me according my node type;
// 2) Current node has attribute or child nodes then continue check me as a tree;
// --------------------------------------------------------- //
procedure TCheckXMLDocThread.CheckXMLNode(myTreeNode:TTreeNode; myXMLNode: IXMLNode; bolDisplayText: Boolean);
var
xmlChildNode: IXMLNode;
xmlAttrNode: IXMLNode;
myChildNode: TTreeNode;
myAttrNode: TTreeNode;
i: Integer;
bolGetText: Boolean;
begin
// If current XML node hasn't any attribute nodes and child nodes then return!
if (myXMLNode.AttributeNodes.Count = 0) and (myXMLNode.ChildNodes.Count = 0) then
begin
if myXMLNode.NodeType = ntText then
begin
myTreeNode.ImageIndex := 3;
myTreeNode.SelectedIndex := 3;
end else if myXMLNode.NodeType = ntCData then
begin
myTreeNode.ImageIndex := 4;
myTreeNode.SelectedIndex := 4;
end else if myXMLNode.NodeType = ntComment then
begin
myTreeNode.ImageIndex := 7;
myTreeNode.SelectedIndex := 7;
end;
if bolDisplayText then
begin
if VarIsNull(myXMLNode.NodeValue) then
myTreeNode.Text := myXMLNode.NodeName + '="NULL"'
else
myTreeNode.Text := myXMLNode.NodeName + '="' + myXMLNode.NodeValue + '"';
end else
myTreeNode.Text := myXMLNode.NodeName;
exit;
end;
for i := 0 to myXMLNode.AttributeNodes.Count - 1 do
begin
xmlAttrNode := myXMLNode.AttributeNodes.Get(i);
if bolDisplayText then
begin
if VarIsNull(xmlAttrNode.NodeValue) then
myAttrNode := frmMain.tvXML.Items.AddChild(myTreeNode, xmlAttrNode.NodeName+'="NULL"')
else
myAttrNode := frmMain.tvXML.Items.AddChild(myTreeNode, xmlAttrNode.NodeName+'="'+xmlAttrNode.NodeValue+'"');
end else
myAttrNode := frmMain.tvXML.Items.AddChild(myTreeNode, xmlAttrNode.NodeName);
myAttrNode.ImageIndex := 5;
myAttrNode.SelectedIndex := 5;
end;
bolGetText := False;
xmlChildNode := myXMLNode.ChildNodes.First;
while(xmlChildNode <> nil) do
begin
if not bolGetText then
begin
myChildNode := frmMain.tvXML.Items.AddChild(myTreeNode, xmlChildNode.NodeName);
myChildNode.ImageIndex := 2;
myChildNode.SelectedIndex := 2;
if xmlChildNode.NodeType = ntText then
bolGetText := True;
end;
if bolDisplayText then
CheckXMLNode(myChildNode, xmlChildNode, True)
else
CheckXMLNode(myChildNode, xmlChildNode, False);
xmlChildNode := xmlChildNode.NextSibling;
end;
end;