XML和对象属性互转的基类
Posted 朝闻道
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了XML和对象属性互转的基类相关的知识,希望对你有一定的参考价值。
本人做了一个XML和对象属性互转的基类,现在放上来有兴趣拿去看一下,用法很简单,声明一个BaseConversion的子类,该子类与你想转换的对象相对应,然后覆盖基类的两个虚方法,然后在里面写元素与对象属性的转换即可以,废话少说了,还是看代码了,例子也懒得去写了,相信不会很难看懂的:
unit XMLConversion;
{*******************************************
* brief: XML和对象属性的转换
* autor: linzhenqun
* date: 2005-10-26
* email: [email protected]
* blog: http://blog.csdn.net/linzhengqun
********************************************}
interface
uses
Classes, SysUtils, xmldom, XMLIntf, oxmldom, XMLDoc;
Type
TBaseConversion = class
protected
FXMLDoc: TXMLDocument;
(* 在ParenElem元素下增加一个子元素 *)
function AppendChildElement(const TagName, Value: WideString;
ParenElem: IDOMElement): IDOMElement;
(* 根据TagName取得XML节点下的某一个子节点 *)
function GetSingleEleByTagName(AEle: IDOMElement; TagName: WideString): IDOMElement;
(* 取得XML节点的文本 *)
function GetElementText(AElement: IDOMNode): WideString;
(* IDOMElement的text值转换成各种类型的数据,
如果AElem=nil或者text不符合某些数据类型,返回Default值 *)
function EleTextToBoolDef(AElem: IDOMElement; const Default: Boolean): Boolean;
function EleTextToStrDef(AElem: IDOMElement; const Default: WideString): WideString;
function EleTextToIntDef(AElem: IDOMElement; const Default: Integer): Integer;
function EleTextToFloatDef(AElem: IDOMElement; const Default: Extended): Extended;
(* 取得IDomElement的属性的值,并转换成各种类型
如果不符合数据类型,返回Default值 *)
function EleAttrToBoolDef(AElem: IDOMElement; AttrName: WideString;
const Default: Boolean): Boolean;
function EleAttrToStrDef(AElem: IDOMElement; AttrName: WideString;
const Default: WideString): WideString;
function EleAttrToIntDef(AElem: IDOMElement; AttrName: WideString;
const Default: Integer): Integer;
function EleAttrToFloatDef(AElem: IDOMElement; AttrName: WideString;
const Default: Extended): Extended;
procedure ObjPropertyToElements(Obj: TObject); virtual;
procedure ElementsToObjProperty(Obj: TObject); virtual;
public
procedure XMLToObjProperty(XMLStream: TStream; Obj: TObject);
procedure ObjPropertyToXML(XMLStream: TStream; Obj: TObject);
constructor Create;
destructor Destroy; override;
end;
implementation
{ TBaseConversion }
function TBaseConversion.AppendChildElement(const TagName,
Value: WideString; 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;
constructor TBaseConversion.Create;
var
Vendor: TDOMVendor;
begin
FXMLDoc := TXMLDocument.Create(nil);
//下面这几句代码可以去掉,则将使用MSXML来解析
//现在是用OpenXML的原生代码解析
Vendor := DOMVendors.Find(‘Open XML‘);
if Vendor <> nil then
FXMLDoc.DOMVendor := Vendor;
end;
destructor TBaseConversion.Destroy;
begin
FXMLDoc.Free;
inherited;
end;
function TBaseConversion.EleAttrToBoolDef(AElem: IDOMElement; AttrName: WideString;
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: WideString;
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: WideString;
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: WideString;
const Default: WideString): WideString;
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;
procedure TBaseConversion.ElementsToObjProperty(Obj: TObject);
begin
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: WideString): WideString;
begin
if AElem <> nil then
Result := GetElementText(AElem)
else
Result := Default;
end;
function TBaseConversion.GetElementText(AElement: IDOMNode): WideString;
begin
Result := AElement.firstChild.nodeValue;
end;
function TBaseConversion.GetSingleEleByTagName(AEle: IDOMElement;
TagName: WideString): IDOMElement;
var
LNodeList: IDOMNodeList;
begin
Result := nil;
LNodeList := AEle.getElementsByTagName(TagName);
if LNodeList <> nil then
Result := LNodeList.item[0] as IDOMElement;
end;
procedure TBaseConversion.ObjPropertyToElements(Obj: TObject);
begin
end;
procedure TBaseConversion.ObjPropertyToXML(XMLStream: TStream;
Obj: TObject);
begin
FXMLDoc.XML.Clear;
FXMLDoc.Active := True;
FXMLDoc.Encoding := ‘utf-8‘;
ObjPropertyToElements(Obj);
FXMLDoc.SaveToStream(XMLStream);
end;
procedure TBaseConversion.XMLToObjProperty(XMLStream: TStream;
Obj: TObject);
begin
FXMLDoc.LoadFromStream(XMLStream);
ElementsToObjProperty(Obj);
end;
end.
http://blog.csdn.net/linzhengqun/article/details/529307
以上是关于XML和对象属性互转的基类的主要内容,如果未能解决你的问题,请参考以下文章