⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmlconversion.pas

📁 一个可以把源代码以语法高亮的形式转换成HTML格式或RTF格式。
💻 PAS
字号:
unit XMLConversion;

{*******************************************
 * brief: XML和对象的转换
 * autor: linzhenqun
 * date: 2005-10-26
 * email: linzhengqun@163.com
 * blog: http://blog.csdn.net/linzhengqun
 * 功能简介:
 *     定义XML与对象转换的框架,转换值留给子类实现
********************************************}

interface
uses
  //Delphi stuff
  Classes, SysUtils, Variants, XMLDoc, xmldom;

Type
  { XML与对象转换基类 }
  TBaseConversion = class
  protected
    FXMLDoc: TXMLDocument;
    (* 在ParenElem元素下增加一个子元素 *)
    function AppendChildElement(const TagName, Value: string;
      ParenElem: IDOMElement): IDOMElement;
    (* 根据TagName取得XML节点下的某一个子节点 *)
    function GetSingleEleByTagName(AEle: IDOMElement; TagName: string): IDOMElement;
    (* 取得XML节点的文本 *)
    function GetElementText(AElement: IDOMNode): string;

    (* IDOMElement的text值转换成各种类型的数据,
       如果AElem=nil或者text不符合某些数据类型,返回Default值 *)
    function EleTextToBoolDef(AElem: IDOMElement; const Default: Boolean): Boolean;
    function EleTextToStrDef(AElem: IDOMElement; const Default: string): string;
    function EleTextToIntDef(AElem: IDOMElement; const Default: Integer): Integer;
    function EleTextToFloatDef(AElem: IDOMElement; const Default: Extended): Extended;

    (* 取得IDomElement的属性的值,并转换成各种类型
       如果不符合数据类型,返回Default值 *)
    function EleAttrToBoolDef(AElem: IDOMElement; AttrName: string;
      const Default: Boolean): Boolean;
    function EleAttrToStrDef(AElem: IDOMElement;  AttrName: string;
      const Default: string): string;
    function EleAttrToIntDef(AElem: IDOMElement;  AttrName: string;
      const Default: Integer): Integer;
    function EleAttrToFloatDef(AElem: IDOMElement;  AttrName: string;
      const Default: Extended): Extended;

    //注: Obj<->XML 这里没有实现,留给子类根据功能来实现
    procedure ObjPropertyToElements(Obj: TObject); virtual;
    procedure ElementsToObjProperty(Obj: TObject); virtual;
    procedure GetDefaultXMLStream(XMLStream: TStream); virtual;
  public
    procedure XMLToObjProperty(XMLStream: TStream; Obj: TObject);
    procedure ObjPropertyToXML(XMLStream: TStream; Obj: TObject);
    constructor Create;
    destructor Destroy; override;
  end;

implementation

{ TBaseConversion }

constructor TBaseConversion.Create;
begin
  FXMLDoc := TXMLDocument.Create(nil);
end;

destructor TBaseConversion.Destroy;
begin
  FXMLDoc.Free;
  inherited;
end;

function TBaseConversion.AppendChildElement(const TagName,
  Value: string; ParenElem: IDOMElement): IDOMElement;
var
  Text: IDOMText;
begin
  Result := FXMLDoc.DOMDocument.createElement(TagName);
  if Value <> '' then
  begin
    Text := FXMLDoc.DOMDocument.createTextNode(Value);
    Result.appendChild(Text);
  end;
  ParenElem.appendChild(Result);
end;

function TBaseConversion.EleAttrToBoolDef(AElem: IDOMElement; AttrName: string;
  const Default: Boolean): Boolean;
var
  LNode: IDOMNode;
begin
  Result := Default;
  if AElem <> nil then
  begin
    LNode := AElem.attributes.getNamedItem(AttrName);
    if LNode <> nil then
      Result := StrToBoolDef(LNode.nodeValue, Default);
  end
end;

function TBaseConversion.EleAttrToFloatDef(AElem: IDOMElement; AttrName: string;
  const Default: Extended): Extended;
var
  LNode: IDOMNode;
begin
  Result := Default;
  if AElem <> nil then
  begin
    LNode := AElem.attributes.getNamedItem(AttrName);
    if LNode <> nil then
      Result := StrToFloatDef(LNode.nodeValue, Default);
  end
end;

function TBaseConversion.EleAttrToIntDef(AElem: IDOMElement; AttrName: string;
  const Default: Integer): Integer;
var
  LNode: IDOMNode;
begin
  Result := Default;
  if AElem <> nil then
  begin
    LNode := AElem.attributes.getNamedItem(AttrName);
    if LNode <> nil then
      Result := StrToIntDef(LNode.nodeValue, Default);
  end
end;

function TBaseConversion.EleAttrToStrDef(AElem: IDOMElement; AttrName: string;
  const Default: string): string;
var
  LNode: IDOMNode;
begin
  Result := Default;
  if AElem <> nil then
  begin
    LNode := AElem.attributes.getNamedItem(AttrName);
    if LNode <> nil then
      Result := LNode.nodeValue;
  end
end;

function TBaseConversion.EleTextToBoolDef(AElem: IDOMElement;
  const Default: Boolean): Boolean;
begin
  if AElem <> nil then
    Result := StrToBoolDef(GetElementText(AElem), Default)
  else
    Result := Default;
end;

function TBaseConversion.EleTextToFloatDef(AElem: IDOMElement;
  const Default: Extended): Extended;
begin
  if AElem <> nil then
    Result := StrToFloatDef(GetElementText(AElem), Default)
  else
    Result := Default;
end;

function TBaseConversion.EleTextToIntDef(AElem: IDOMElement;
  const Default: Integer): Integer;
begin
  if AElem <> nil then
    Result := StrToIntDef(GetElementText(AElem), Default)
  else
    Result := Default;
end;

function TBaseConversion.EleTextToStrDef(AElem: IDOMElement;
  const Default: string): string;
begin
  if AElem <> nil then
    Result := GetElementText(AElem)
  else
    Result := Default;
end;


function TBaseConversion.GetElementText(AElement: IDOMNode): string;
begin
  Result := AElement.firstChild.nodeValue;
end;

function TBaseConversion.GetSingleEleByTagName(AEle: IDOMElement;
  TagName: string): IDOMElement;
var
  LNodeList: IDOMNodeList;
begin
  Result := nil;
  LNodeList := AEle.getElementsByTagName(TagName);
  if LNodeList <> nil then
    Result := LNodeList.item[0] as IDOMElement;
end;

procedure TBaseConversion.GetDefaultXMLStream(XMLStream: TStream);
begin
end;  
procedure TBaseConversion.ObjPropertyToElements(Obj: TObject);
begin
end;
procedure TBaseConversion.ElementsToObjProperty(Obj: TObject);
begin
end;

procedure TBaseConversion.ObjPropertyToXML(XMLStream: TStream;
  Obj: TObject);
var
  XML: WideString;
begin
  FXMLDoc.XML.Clear;
  FXMLDoc.Active := True;
  FXMLDoc.Encoding := 'UTF-8';
  ObjPropertyToElements(Obj);
  FXMLDoc.SaveToXML(XML);
  FXMLDoc.LoadFromXML(FormatXMLData(XML));
  FXMLDoc.SaveToStream(XMLStream);
end;

procedure TBaseConversion.XMLToObjProperty(XMLStream: TStream;
  Obj: TObject);
begin
  FXMLDoc.LoadFromStream(XMLStream);
  ElementsToObjProperty(Obj);
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -