5,379
社区成员
发帖
与我相关
我的任务
分享
type
TXMLOption=class
private
FActive :boolean;
FFilename: string;
FXMLDoc :IXMLDOMDocument;
//填加一个子节点
procedure AddSimpleElement(Parent: IXMLDOMElement; Field,Value: string);
public
procedure CreateBlank(Filename: string);
procedure OpenXml(Filename: string);
procedure CloseXml;
procedure AppendProtocol(newptl: RecProtocol);
end;
implementation
const
XMLTag = 'xml';
XMLPrologAttrs = 'version="1.0" encoding="UTF-8"';
XMLComment = '主站规约库';
ProtocolTag = 'protocol_list';
ItemTag = 'protocol';
XMLComment2 = '创建文档时间:';
XML_TypeId = 'typeid';
XML_Id = 'id';
XML_Name = 'name';
XML_CtrlCode = 'ctrlcode';
XML_FunCode = 'funcode';
XML_DataLength = 'datalength';
XML_DataFormat = 'dataformat';
XML_Unit = 'unit';
XML_InitValue = 'initvalue';
XML_RwFlag = 'rwflag';
XML_LPwd = 'lpwd';
XML_HPwd = 'hpwd';
XML_Help = 'help';
//创建一个空XML,如果这个Filename文件已经存在,则覆盖
procedure TXMLOption.CreateBlank(Filename: string);
begin
FActive:=false;
FFilename:='';
try
FXMLDoc := CoDOMDocument.Create;
FXMLDoc.AppendChild(FXMLDoc.CreateProcessingInstruction(XMLTag, XMLPrologAttrs)); //表头
FXMLDoc.AppendChild(FXMLDoc.CreateComment(XMLComment)); //文档说明
FXMLDoc.AppendChild(FXMLDoc.CreateElement(ProtocolTag)); //元素
FXMLDoc.AppendChild(FXMLDoc.CreateComment(XMLComment2+datetimetostr(now))); //文档结尾
FXMLDoc.save(Filename);
FFilename:=Filename;
FActive:=true;
except
FXMLDoc:=nil;
end;
end;
//打开一个存在的Filename XML文档
procedure TXMLOption.OpenXml(Filename: string);
begin
if not Assigned(FXMLDoc) then
begin
FXMLDoc := CoDOMDocument.Create;
if FXMLDoc.Load(Filename) then FActive:=true
else FActive:=false;
if FActive then FFilename:=Filename
else FFilename:='';
end;
end;
//关闭一个打开的XML文档
procedure TXMLOption.CloseXml;
begin
if Assigned(FXMLDoc) then FXMLDoc:=nil;
FFilename:='';
FActive:=false;
end;
procedure TXMLOption.AddSimpleElement(Parent: IXMLDOMElement; Field,Value: string);
var
Internal: IXMLDOMElement;
begin
Internal:=IXMLDOMElement(Parent.AppendChild(FXMLDoc.CreateElement(Field)));
Internal.AppendChild(FXMLDoc.CreateTextNode(Value));
end;
//填加一个节点到后面
procedure TXMLOption.AppendProtocol(newptl: RecProtocol);
var
xnode:IXMLDOMElement;
xroot:IXMLDOMElement;
begin
if FActive then
begin
xroot:=FXMLDoc.documentElement;
xnode :=IXMLDOMElement(xroot.AppendChild(FXMLDoc.CreateElement(ItemTag)));
xnode.setAttribute(XML_TypeId, newptl.P_TypeId);
AddSimpleElement(xnode, XML_Id, newptl.P_Id);
AddSimpleElement(xnode, XML_Name, newptl.P_Name);
AddSimpleElement(xnode, XML_CtrlCode, newptl.P_CtrlCode);
AddSimpleElement(xnode, XML_FunCode, newptl.P_FunCode);
AddSimpleElement(xnode, XML_DataLength, newptl.P_DataLength);
AddSimpleElement(xnode, XML_DataFormat, newptl.P_DataFormat);
AddSimpleElement(xnode, XML_Unit, newptl.P_Unit);
AddSimpleElement(xnode, XML_InitValue, newptl.P_InitValue);
AddSimpleElement(xnode, XML_RwFlag, newptl.P_RwFlag);
AddSimpleElement(xnode, XML_LPwd, newptl.P_LPwd);
AddSimpleElement(xnode, XML_HPwd, newptl.P_HPwd);
AddSimpleElement(xnode, XML_Help, newptl.P_Help);
FXMLDoc.save(FFilename);
end;
end;
procedure PrettyFormat(const AXMLDoc: IXMLDOMDocument);
procedure TraverseNode(Node: IXMLDOMNode; Indent: string);
const
IndentLevel = #9;
var
LineBreak: IXMLDOMNode;
ChildNode, NextNode: IXMLDOMNode;
AnyChildNode: Boolean;
begin
if Node = nil then
Exit;
AnyChildNode:= False;
ChildNode:= Node.Get_firstChild;
while ChildNode <> nil do
begin
NextNode:= ChildNode.nextSibling;
if ChildNode.nodeType = NODE_ELEMENT then
begin
AnyChildNode:= True;
// Insert LineBreak before each child Node
LineBreak:= FXMLDoc.createTextNode(sLineBreak +
Indent + IndentLevel);
Node.insertBefore(LineBreak, ChildNode);
TraverseNode(ChildNode, Indent + IndentLevel);
end;
ChildNode:= NextNode;
end;
if (Node.nodeType = NODE_ELEMENT) and AnyChildNode then
begin
// Add LineBreak after Node
ChildNode:= NextNode;
LineBreak:= FXMLDoc.createTextNode(sLineBreak + Indent);
Node.appendChild(LineBreak);
end;
end;
begin
TraverseNode(AXMLDoc.documentElement, '');
end;