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('没有根节点');
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('不是数值');
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.
Delphi XML 增删改查
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 为什么会有dom,dom4j这些解析xml的工具呢?下面是百度的。Dom4j JDom等xml解析工具定义了对xm...
- 0. Thanks Android解析XML的三种方式Android几种解析XML方式的比较android xml...
- 0. Thanks Android解析XML的三种方式Android几种解析XML方式的比较android xml...