Delphi XML 增刪改查

unit XML;

interface

uses
  XMLDoc, XMLIntf, XMLDom, SysUtils, Variants;

type
  TNode = class
  private
    _XML: IXMLDocument;
    FXML: TXMLDocument;
    FrootNode: IXMLNode;
    _vPropName: string;
    function FindNode(const NodeName: string): IXMLNode;
    function GetAsString: string;
    procedure SetAsString(Value: string);
    Function GetAsInteger: Integer;
    procedure SetAsInteger(value: integer);
    function GetAsChildNode: IXMLNode;
    procedure SetAsChildNode(Value: IXMLNode);
  public

    property AsChildNode: IXMLNode read GetAsChildNode write SetAschildNode;
    property AsInteger: Integer read GetAsInteger write SetAsInteger;
    property AsString: string read GetAsString write SetAsString;
    procedure del;
    constructor create;
    destructor destroy; override;
  end;

type
  TXML = class(TNode)
  private
    _XMLStr: String;
  public
    function Text: string;
    function NodeByName(NodeName: string): TNode;
    procedure Init;
    constructor create; overload;
    constructor create(XMLStr: string); overload;
    destructor destroy; override;
  end;

implementation

uses
  Xml.omnixmldom;

{ TXML }

constructor TXML.create;
begin
  Inherited;


end;

constructor TXML.create(XMLStr: string);
begin

end;




destructor TXML.destroy;
begin

  inherited;
end;

procedure TXML.Init;
begin
  _XMLStr := _XML.XML.Text;
end;

function TNode.FindNode(const NodeName: string): IXMLNode;
var
  _RootNode: IXMLNode;
begin
  result := nil;
  _RootNode := _XML.DocumentElement;
  if _RootNode = nil then
  begin
    raise Exception.Create('沒有根節(jié)點');
    exit;
  end;
  result := _RootNode.ChildNodes.FindNode(NodeName);
end;

function TXML.NodeByName(NodeName: string): TNode;
begin
  _vPropName := NodeName;
  FRootNode := FindNode(NodeName);
  result := Self;
end;

function TXML.Text: string;
begin
  result := _XMLStr;
end;

{ TNode }

constructor TNode.create;
var
  _RootNode: IXMLNode;
begin
  FXML := TXMLDocument.Create(nil);
  _XML := FXML;
  FXML.DOMVendor := GetDOMVendor('Omni XML');
  _XML.Active := True;
  _RootNode := _XML.CreateNode('xml');
  _XML.DocumentElement := _RootNode;
end;

procedure TNode.del;
var
  NodeList: IXMLNodeList;
  _Node: IXMLNode;
begin
  NodeList := _XML.DocumentElement.ChildNodes;
  _Node := NodeList.FindNode(_vPropName);
  NodeList.Remove(_Node);
end;


destructor TNode.destroy;
begin

end;

function TNode.GetAsChildNode: IXMLNode;
begin
  result := FRootNode;
end;

function TNode.GetAsInteger: Integer;
var
  tmp: string;
begin
  tmp := VarToStrDef(FRootNode.NodeValue, '0');
  if not TryStrToInt(tmp, result) then
    raise Exception.Create('不是數(shù)值');
end;

function TNode.GetAsString: string;
var
  _TmpNode: IXMLNode;
begin
  result := VarToStrDef(FrootNode.NodeValue, '');
end;

procedure TNode.SetAsChildNode(Value: IXMLNode);
var
  _RootNode: IXMLNode;
begin
  _RootNode := Value;
  if FRootNode = nil then
  begin
    _RootNode := _XML.CreateNode(_vPropName);
    FRootNode := _XML.DocumentElement;
    _RootNode.ChildNodes.Add(Value);
    FRootNode.ChildNodes.Add(_RootNode)
  end
  else begin
    FRootNode.ChildNodes.Add(_RootNode);
  end;
end;

procedure TNode.SetAsInteger(value: integer);
var
  _ChildNode: IXMLNode;
  _rootNode: IXMLNode;
begin
  if FRootNode = nil then
  begin
    _RootNode := _XML.CreateNode(_vPropName);
    FRootNode := _XML.DocumentElement;
    _ChildNode := _XML.CreateNode(IntToStr(Value), ntText);
    _RootNode.ChildNodes.Add(_ChildNode);
    FRootNode.ChildNodes.Add(_RootNode)
  end
  else begin
    FRootNode.Text := IntToStr(value);
  end;

end;

procedure TNode.SetAsString(Value: string);
var
  _ChildNode: IXMLNode;
  _rootNode: IXMLNode;
begin
  if FrootNode = nil then
  begin
    _RootNode := _XML.CreateNode(_vPropName);
    FRootNode := _XML.DocumentElement;
    _ChildNode := _XML.CreateNode(Value, ntCData); //_vPropName);
    _RootNode.ChildNodes.add(_ChildNode);
    FrootNode.ChildNodes.Add(_RootNode);

  end
  else begin

    FRootNode.Text := Value;
  end;
end;


end.

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末灭返,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子励翼,更是在濱河造成了極大的恐慌骚亿,老刑警劉巖椭符,帶你破解...
    沈念sama閱讀 216,744評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件渐尿,死亡現(xiàn)場離奇詭異历极,居然都是意外死亡洒缀,警方通過查閱死者的電腦和手機(jī)哭靖,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,505評論 3 392
  • 文/潘曉璐 我一進(jìn)店門具垫,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人试幽,你說我怎么就攤上這事筝蚕。” “怎么了铺坞?”我有些...
    開封第一講書人閱讀 163,105評論 0 353
  • 文/不壞的土叔 我叫張陵起宽,是天一觀的道長。 經(jīng)常有香客問我济榨,道長坯沪,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,242評論 1 292
  • 正文 為了忘掉前任擒滑,我火速辦了婚禮腐晾,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘橘忱。我一直安慰自己赴魁,他們只是感情好卸奉,可當(dāng)我...
    茶點故事閱讀 67,269評論 6 389
  • 文/花漫 我一把揭開白布钝诚。 她就那樣靜靜地躺著,像睡著了一般榄棵。 火紅的嫁衣襯著肌膚如雪凝颇。 梳的紋絲不亂的頭發(fā)上潘拱,一...
    開封第一講書人閱讀 51,215評論 1 299
  • 那天,我揣著相機(jī)與錄音拧略,去河邊找鬼芦岂。 笑死,一個胖子當(dāng)著我的面吹牛垫蛆,可吹牛的內(nèi)容都是我干的禽最。 我是一名探鬼主播,決...
    沈念sama閱讀 40,096評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼袱饭,長吁一口氣:“原來是場噩夢啊……” “哼川无!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起虑乖,我...
    開封第一講書人閱讀 38,939評論 0 274
  • 序言:老撾萬榮一對情侶失蹤懦趋,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后疹味,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體仅叫,經(jīng)...
    沈念sama閱讀 45,354評論 1 311
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,573評論 2 333
  • 正文 我和宋清朗相戀三年糙捺,在試婚紗的時候發(fā)現(xiàn)自己被綠了诫咱。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,745評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡继找,死狀恐怖遂跟,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情婴渡,我是刑警寧澤幻锁,帶...
    沈念sama閱讀 35,448評論 5 344
  • 正文 年R本政府宣布,位于F島的核電站边臼,受9級特大地震影響哄尔,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜柠并,卻給世界環(huán)境...
    茶點故事閱讀 41,048評論 3 327
  • 文/蒙蒙 一岭接、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧臼予,春花似錦鸣戴、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,683評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至缰雇,卻和暖如春入偷,著一層夾襖步出監(jiān)牢的瞬間追驴,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,838評論 1 269
  • 我被黑心中介騙來泰國打工疏之, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留殿雪,地道東北人。 一個月前我還...
    沈念sama閱讀 47,776評論 2 369
  • 正文 我出身青樓锋爪,卻偏偏與公主長得像丙曙,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子其骄,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,652評論 2 354

推薦閱讀更多精彩內(nèi)容