unit Leading_XML;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, kbmMemTable, kbmMemTableHH, ActnList, ImgList,
Excel97, OleServer, ComCtrls, ToolWin, ExtCtrls,xmldoc,XMLIntf,msxmldom,
xmldom;
type
THHLeading_XML = class(TForm)
Panel2: TPanel;
Label7: TLabel;
Panel3: TPanel;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
BtnChooseExcel: TButton;
BtnLeading_In_Database: TButton;
ToolBar3: TToolBar;
btnBack: TToolButton;
Panel1: TPanel;
OpenDialog1: TOpenDialog;
ImageList1: TImageList;
ActionList1: TActionList;
act_Back: TAction;
ChooseXml: TAction;
Leading_In_Database: TAction;
MMTable: TkbmMemTableHH;
Memo1: TMemo;
XMLDocument1: TXMLDocument;
procedure act_BackExecute(Sender: TObject);
procedure ChooseXmlExecute(Sender: TObject);
procedure Leading_In_DatabaseExecute(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
protected
{ ICommonService methods }
function GetXMLNodeSpecialvalue(strEntityEnginefile:String;
XMLNodePath:String;const XMLAttrName:String='';const XMLSpecialName:String='';
const XMLSpecialvalue:String=''; const dep:Char ='.'):String;
function getnodefromIXMLNodeList(childnodes:IXMLNodeList;nodename:String):IXMLNode;
end;
var
HHLeading_XML: THHLeading_XML;
implementation
{$R *.dfm}
uses NativeXml;
const xmlFile = 'C:/Temp/xml_test_files/VendorContrac.xml';
procedure THHLeading_XML.act_BackExecute(Sender: TObject);
begin
//
close;
end;
procedure THHLeading_XML.ChooseXmlExecute(Sender: TObject);
var
ADoc: TNativeXml;
url:string;
node: IXMLNode;
begin
opendialog1.InitialDir := ExtractFileDir(paramstr(0)); //文件的打存放初始路径
opendialog1.Execute;
url:=opendialog1.filename;
//XMLDocument1 := TNativeXml.Create;
//XMLDocument1.Utf8Encoded := True;
XMLDocument1.LoadFromFile(url);
//EncodingString:='gb2312';
Memo1.Lines := XMLDocument1.XML; {查看}
//ADoc := TNativeXml.Create;
//ADoc.LoadFromFile(url);
//Memo1.Text := ADoc.WriteToString;
//ADoc.Free;
//获取节点名称
node := XMLDocument1.DocumentElement;
ShowMessage(node.NodeName); {科室名单}
node := node.ChildNodes[0]; {XMLDocument1.DocumentElement.ChildNodes[0]}
ShowMessage(node.NodeName); {人员}
//node := node.ChildNodes[0]; {XMLDocument1.DocumentElement.ChildNodes[0].ChildNodes[0]}
//ShowMessage(node.NodeName); {姓名}
//node := node.ChildNodes[0].ChildNodes[0];
//ShowMessage(node.NodeName);
//获取属性名称
//node := XMLDocument1.DocumentElement;
ShowMessage(node.AttributeNodes[0].NodeName); {备注}
//node := node.ChildNodes[0]; {XMLDocument1.DocumentElement.ChildNodes[0]}
//ShowMessage(node.AttributeNodes[0].NodeName); {职务}
GetXMLNodeSpecialvalue();
end;
procedure THHLeading_XML.Leading_In_DatabaseExecute(Sender: TObject);
begin
//
ShowMessage('导入成功');
end;
procedure THHLeading_XML.FormCreate(Sender: TObject);
begin
//Memo1.Lines.LoadFromFile(xmlFile);
end;
{-------------------------------------------------------------------------------
Fun/Pro: GetXMLNodeSpecialvalue
@Date: 2011.06.29
@Param: xmlFile xml文件
@Param: xmlnodepath 节点
@Param: xmlattrname 节点中的属性名称,如果直接取节点值则可以忽略此参数。
@Param: XMLSpecialName 要查找的节点中属性名
@Param: XMLSpecialvalue 要查找的节点中某属性对应的值
@Param: dep 节点的参数的分隔符,默认为.
@Return: 某属性的值
-------------------------------------------------------------------------------}
function THHLeading_XML.GetXMLNodeSpecialvalue(strEntityEnginefile:String; XMLNodePath:String;
const XMLAttrName:String=''; const XMLSpecialName:String=''; const XMLSpecialvalue:String=''; const dep:Char ='.'):String;
var
xmlDocument :IXMLDocument;
node :IXMLNode;
xmlnodeList :TStrings;
i :Integer;
urlcount :Integer;
begin
//xml节点路径
xmlnodeList:=TStringList.Create;
xmlnodeList.Delimiter:=dep;
xmlnodeList.DelimitedText:=xmlnodepath;
urlcount:=xmlnodeList.Count;
//xml对象
xmlDocument :=TXMLDocument.Create(nil);
xmlDocument.LoadFromFile(strEntityEngineFile);
xmlDocument.Active:=true;
try
node:= xmlDocument.DocumentElement;
if(node.NodeName = xmlnodeList[0]) then begin
//扫描节点
for i := 1 to urlcount-1 do begin
if(node<>nil) then
begin
node := getnodefromIXMLNodeList(node.ChildNodes,xmlnodeList[i]);
end
else Break;
end;
if(node=nil)then begin
result:='';
end else begin
//判断是取属性还是取节点内容
if(Trim(xmlattrname)='') then
result:=node.Text
else
begin
result := node.AttributeNodes.Nodes[XMLSpecialName].Nodevalue; //这里不想再声明一个临时变量了,就用result来比较,可能有隐患。
while ((result <> XMLSpecialvalue)) do
begin
node := node.NextSibling;
while (node.NodeName = '#comment') do
begin
node:= node.NextSibling;
end;
result := node.AttributeNodes.Nodes[XMLSpecialName].Nodevalue;
end;
result:=node.AttributeNodes.Nodes[XMLAttrName].Nodevalue;
end;
end;
end else begin
result:='';
end;
except
result:='error';
end;
xmlDocument.Active:=false;
end;
function THHLeading_XML.getnodefromIXMLNodeList(childnodes:IXMLNodeList;nodename:String):IXMLNode;
var
i: Integer;
begin
for i :=1 to childnodes.Count do begin
if(childnodes.Get(i-1).NodeName = nodename) then begin
result:= childnodes[i-1];
exit;
end;
end;
end;
end.