unit uutlXmlHelper; {$mode objfpc}{$H+} { *************************** EXAMPLE ********************************* doc := TXMLDocument.Create; try root := doc.CreateElement('root'); doc.AppendChild(root); with TutlXmlHelper.Create(root) do begin SetAttribString('test', 'blubb'); SetAttribInt('new', 123); with TutlXmlHelper.Create(AppendNode('test')) do begin SetString('child node :)'); end; with TutlXmlHelper.Create(AppendNode('test')) do begin SetString('another child node :)'); end; end; WriteXMLFile(doc, 'save.xml'); finally FreeAndNil(doc); end; } interface uses Classes, SysUtils, DOM; type ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// IutlNodeEnumerator = interface(specialize IEnumerator) ['{59F90C67-8A0B-48AC-8A49-D39317A07FE2}'] function GetEnumerator: IutlNodeEnumerator; end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// IutlXmlHelper = interface(IUnknown) ['{C489CEC8-BAD6-4E03-A554-9DF31E84CDBB}'] // simple properties function GetElement: TDOMElement; property Element: TDOMElement read GetElement; // set value of current node procedure SetString (const aValue: String); overload; procedure SetString (const aValue: WideString); overload; procedure SetString (const aValue: UnicodeString); overload; procedure SetInt (const aValue: Integer); procedure SetFloat (const aValue: Double); procedure SetBool (const aValue: Boolean); // get value of current node function GetString (const aDefault: String = ''): String; function GetStringW (const aDefault: WideString = ''): WideString; function GetStringU (const aDefault: UnicodeString = ''): UnicodeString; function GetInt (const aDefault: Int64 = 0): Int64; function GetFloat (const aDefault: Double = 0): Double; function GetBool (const aDefault: Boolean = false): Boolean; // try get value of current node function TryGetString (out aValue: String): Boolean; function TryGetStringW (out aValue: WideString): Boolean; function TryGetStringU (out aValue: UnicodeString): Boolean; function TryGetInt (out aValue: Int64): Boolean; function TryGetFloat (out aValue: Double): Boolean; function TryGetBool (out aValue: Boolean): Boolean; // set value of attribute procedure SetAttribString (const aName: DOMString; const aValue: String); overload; procedure SetAttribString (const aName: DOMString; const aValue: WideString); overload; procedure SetAttribString (const aName: DOMString; const aValue: UnicodeString); overload; procedure SetAttribInt (const aName: DOMString; const aValue: Integer); overload; procedure SetAttribFloat (const aName: DOMString; const aValue: Double); procedure SetAttribBool (const aName: DOMString; const aValue: Boolean); // get value of attribute function GetAttribString (const aName: DOMString; const aDefault: String = ''): String; function GetAttribStringW (const aName: DOMString; const aDefault: WideString = ''): WideString; function GetAttribStringU (const aName: DOMString; const aDefault: UnicodeString = ''): UnicodeString; function GetAttribInt (const aName: DOMString; const aDefault: Int64 = 0): Int64; function GetAttribFloat (const aName: DOMString; const aDefault: Double = 0): Double; function GetAttribBool (const aName: DOMString; const aDefault: Boolean = false): Boolean; // get value of attribute function TryGetAttribString (const aName: DOMString; out aValue: String): Boolean; function TryGetAttribStringW (const aName: DOMString; out aValue: WideString): Boolean; function TryGetAttribStringU (const aName: DOMString; out aValue: UnicodeString): Boolean; function TryGetAttribInt (const aName: DOMString; out aValue: Int64): Boolean; function TryGetAttribFloat (const aName: DOMString; out aValue: Double): Boolean; function TryGetAttribBool (const aName: DOMString; out aValue: Boolean): Boolean; // node operations function Nodes (const aName: DOMString = ''): IutlNodeEnumerator; function Node (const aName: DOMString; const aCanCreate: Boolean = true): TDOMElement; function PrependNode (const aName: DOMString): TDOMElement; function AppendNode (const aName: DOMString): TDOMElement; procedure PrependText (const aText: DOMString); procedure AppendText (const aText: DOMString); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// TutlNodeEnumerator = class(TInterfacedObject, IutlNodeEnumerator) private fIndex: Integer; fName: DOMString; fParent: TDOMElement; public { INodeEnumerator } function GetEnumerator: IutlNodeEnumerator; function GetCurrent: TDOMElement; function MoveNext: Boolean; procedure Reset; public constructor Create(const aParent: TDOMElement; const aName: DOMString); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// TutlXmlHelper = class(TInterfacedObject, IutlXmlHelper) private fElement: TDOMElement; public { IutlXmlHelper } function GetElement: TDOMElement; // set value of current node procedure SetString (const aValue: String); overload; procedure SetString (const aValue: WideString); overload; procedure SetString (const aValue: UnicodeString); overload; procedure SetInt (const aValue: Integer); procedure SetFloat (const aValue: Double); procedure SetBool (const aValue: Boolean); // get value of current node function GetString (const aDefault: String): String; function GetStringW (const aDefault: WideString): WideString; function GetStringU (const aDefault: UnicodeString): UnicodeString; function GetInt (const aDefault: Int64): Int64; function GetFloat (const aDefault: Double): Double; function GetBool (const aDefault: Boolean): Boolean; // try get value of current node function TryGetString (out aValue: String): Boolean; function TryGetStringW (out aValue: WideString): Boolean; function TryGetStringU (out aValue: UnicodeString): Boolean; function TryGetInt (out aValue: Int64): Boolean; function TryGetFloat (out aValue: Double): Boolean; function TryGetBool (out aValue: Boolean): Boolean; // set value of attribute procedure SetAttribString (const aName: DOMString; const aValue: String); overload; procedure SetAttribString (const aName: DOMString; const aValue: WideString); overload; procedure SetAttribString (const aName: DOMString; const aValue: UnicodeString); overload; procedure SetAttribInt (const aName: DOMString; const aValue: Integer); overload; procedure SetAttribFloat (const aName: DOMString; const aValue: Double); procedure SetAttribBool (const aName: DOMString; const aValue: Boolean); // get value of attribute function GetAttribString (const aName: DOMString; const aDefault: String): String; function GetAttribStringW (const aName: DOMString; const aDefault: WideString): WideString; function GetAttribStringU (const aName: DOMString; const aDefault: UnicodeString): UnicodeString; function GetAttribInt (const aName: DOMString; const aDefault: Int64): Int64; function GetAttribFloat (const aName: DOMString; const aDefault: Double): Double; function GetAttribBool (const aName: DOMString; const aDefault: Boolean): Boolean; // get value of attribute function TryGetAttribString (const aName: DOMString; out aValue: String): Boolean; function TryGetAttribStringW(const aName: DOMString; out aValue: WideString): Boolean; function TryGetAttribStringU(const aName: DOMString; out aValue: UnicodeString): Boolean; function TryGetAttribInt (const aName: DOMString; out aValue: Int64): Boolean; function TryGetAttribFloat (const aName: DOMString; out aValue: Double): Boolean; function TryGetAttribBool (const aName: DOMString; out aValue: Boolean): Boolean; // node operations function Nodes (const aName: DOMString): IutlNodeEnumerator; function Node (const aName: DOMString; const aCanCreate: Boolean = true): TDOMElement; function PrependNode (const aName: DOMString): TDOMElement; function AppendNode (const aName: DOMString): TDOMElement; procedure PrependText (const aText: DOMString); procedure AppendText (const aText: DOMString); private {%H-}constructor Create; reintroduce; {%H-}constructor Create(const aElement: TDOMElement); reintroduce; public class function Create(const aElement: TDOMElement): IutlXmlHelper; public // set value of current node class function SetString (const aNode: TDOMNode; const aValue: String): TDOMNode; overload; class function SetString (const aNode: TDOMNode; const aValue: WideString): TDOMNode; overload; class function SetString (const aNode: TDOMNode; const aValue: UnicodeString): TDOMNode; overload; class function SetInt (const aNode: TDOMNode; const aValue: Integer): TDOMNode; overload; class function SetFloat (const aNode: TDOMNode; const aValue: Double): TDOMNode; class function SetBool (const aNode: TDOMNode; const aValue: Boolean): TDOMNode; // get value of current node class function GetString (const aNode: TDOMNode; const aDefault: String): String; class function GetStringW (const aNode: TDOMNode; const aDefault: WideString): WideString; class function GetStringU (const aNode: TDOMNode; const aDefault: UnicodeString): UnicodeString; class function GetInt (const aNode: TDOMNode; const aDefault: Int64): Int64; class function GetFloat (const aNode: TDOMNode; const aDefault: Double): Double; class function GetBool (const aNode: TDOMNode; const aDefault: Boolean): Boolean; // try get value of current node class function TryGetString (const aNode: TDOMNode; out aValue: String): Boolean; class function TryGetStringW (const aNode: TDOMNode; out aValue: WideString): Boolean; class function TryGetStringU (const aNode: TDOMNode; out aValue: UnicodeString): Boolean; class function TryGetInt (const aNode: TDOMNode; out aValue: Int64): Boolean; class function TryGetFloat (const aNode: TDOMNode; out aValue: Double): Boolean; class function TryGetBool (const aNode: TDOMNode; out aValue: Boolean): Boolean; // set value of attribute class procedure SetAttribString (const aNode: TDOMElement; const aName: DOMString; const aValue: String); overload; class procedure SetAttribString (const aNode: TDOMElement; const aName: DOMString; const aValue: WideString); overload; class procedure SetAttribString (const aNode: TDOMElement; const aName: DOMString; const aValue: UnicodeString); overload; class procedure SetAttribInt (const aNode: TDOMElement; const aName: DOMString; const aValue: Integer); overload; class procedure SetAttribFloat (const aNode: TDOMElement; const aName: DOMString; const aValue: Double); class procedure SetAttribBool (const aNode: TDOMElement; const aName: DOMString; const aValue: Boolean); // get value of attribute class function GetAttribString (const aNode: TDOMElement; const aName: DOMString; const aDefault: String = ''): String; class function GetAttribStringW (const aNode: TDOMElement; const aName: DOMString; const aDefault: WideString = ''): WideString; class function GetAttribStringU (const aNode: TDOMElement; const aName: DOMString; const aDefault: UnicodeString = ''): UnicodeString; class function GetAttribInt (const aNode: TDOMElement; const aName: DOMString; const aDefault: Int64 = 0): Int64; class function GetAttribFloat (const aNode: TDOMElement; const aName: DOMString; const aDefault: Double = 0): Double; class function GetAttribBool (const aNode: TDOMElement; const aName: DOMString; const aDefault: Boolean = false): Boolean; // get value of attribute class function TryGetAttribString (const aNode: TDOMElement; const aName: DOMString; out aValue: String): Boolean; class function TryGetAttribStringW (const aNode: TDOMElement; const aName: DOMString; out aValue: WideString): Boolean; class function TryGetAttribStringU (const aNode: TDOMElement; const aName: DOMString; out aValue: UnicodeString): Boolean; class function TryGetAttribInt (const aNode: TDOMElement; const aName: DOMString; out aValue: Int64): Boolean; class function TryGetAttribFloat (const aNode: TDOMElement; const aName: DOMString; out aValue: Double): Boolean; class function TryGetAttribBool (const aNode: TDOMElement; const aName: DOMString; out aValue: Boolean): Boolean; // node operations class function Nodes (const aElement: TDOMElement; const aName: DOMString = ''): IutlNodeEnumerator; class function Node (const aElement: TDOMElement; const aName: DOMString; const aCanCreate: Boolean = true): TDOMElement; class function PrependNode (const aElement: TDOMElement; const aName: DOMString): TDOMElement; class function AppendNode (const aElement: TDOMElement; const aName: DOMString): TDOMElement; class procedure PrependText (const aElement: TDOMElement; const aText: DOMString); class procedure AppendText (const aElement: TDOMElement; const aText: DOMString); end; implementation ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //TutlNodeEnumerator///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TutlNodeEnumerator.GetEnumerator: IutlNodeEnumerator; begin result := self; end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TutlNodeEnumerator.GetCurrent: TDOMElement; begin result := (fParent.ChildNodes[fIndex] as TDOMElement); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TutlNodeEnumerator.MoveNext: Boolean; var c: Integer; begin c := fParent.ChildNodes.Count; repeat inc(fIndex) until (fIndex >= c) or ( (fParent.ChildNodes[fIndex] is TDOMElement) and ( (fName = '') or (fName = fParent.ChildNodes[fIndex].NodeName))); result := (fIndex < c); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlNodeEnumerator.Reset; begin fIndex := -1; end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// constructor TutlNodeEnumerator.Create(const aParent: TDOMElement; const aName: DOMString); begin inherited Create; fParent := aParent; fName := aName; fIndex := -1; end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //TutlXmlHelper////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TutlXmlHelper.GetElement: TDOMElement; begin result := fElement; end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlXmlHelper.SetString(const aValue: String); begin TutlXmlHelper.SetString(fElement, aValue); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlXmlHelper.SetString(const aValue: WideString); begin TutlXmlHelper.SetString(fElement, aValue); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlXmlHelper.SetString(const aValue: UnicodeString); begin TutlXmlHelper.SetString(fElement, aValue); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlXmlHelper.SetInt(const aValue: Integer); begin TutlXmlHelper.SetInt(fElement, aValue); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlXmlHelper.SetFloat(const aValue: Double); begin TutlXmlHelper.SetFloat(fElement, aValue); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlXmlHelper.SetBool(const aValue: Boolean); begin TutlXmlHelper.SetBool(fElement, aValue); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TutlXmlHelper.GetString(const aDefault: String): String; begin result := TutlXmlHelper.GetString(fElement, aDefault); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TutlXmlHelper.GetStringW(const aDefault: WideString): WideString; begin result := TutlXmlHelper.GetStringW(fElement, aDefault); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TutlXmlHelper.GetStringU(const aDefault: UnicodeString): UnicodeString; begin result := TutlXmlHelper.GetStringU(fElement, aDefault); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TutlXmlHelper.GetInt(const aDefault: Int64): Int64; begin result := TutlXmlHelper.GetInt(fElement, aDefault); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TutlXmlHelper.GetFloat(const aDefault: Double): Double; begin result := TutlXmlHelper.GetFloat(fElement, aDefault); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TutlXmlHelper.GetBool(const aDefault: Boolean): Boolean; begin result := TutlXmlHelper.GetBool(fElement, aDefault); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TutlXmlHelper.TryGetString(out aValue: String): Boolean; begin result := TutlXmlHelper.TryGetString(fElement, aValue); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TutlXmlHelper.TryGetStringW(out aValue: WideString): Boolean; begin result := TutlXmlHelper.TryGetStringW(fElement, aValue); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TutlXmlHelper.TryGetStringU(out aValue: UnicodeString): Boolean; begin result := TutlXmlHelper.TryGetStringU(fElement, aValue); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TutlXmlHelper.TryGetInt(out aValue: Int64): Boolean; begin result := TutlXmlHelper.TryGetInt(fElement, aValue); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TutlXmlHelper.TryGetFloat(out aValue: Double): Boolean; begin result := TutlXmlHelper.TryGetFloat(fElement, aValue); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TutlXmlHelper.TryGetBool(out aValue: Boolean): Boolean; begin result := TutlXmlHelper.TryGetBool(fElement, aValue); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlXmlHelper.SetAttribString(const aName: DOMString; const aValue: String); begin TutlXmlHelper.SetAttribString(fElement, aName, aValue); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlXmlHelper.SetAttribString(const aName: DOMString; const aValue: WideString); begin TutlXmlHelper.SetAttribString(fElement, aName, aValue); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlXmlHelper.SetAttribString(const aName: DOMString; const aValue: UnicodeString); begin TutlXmlHelper.SetAttribString(fElement, aName, aValue); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlXmlHelper.SetAttribInt(const aName: DOMString; const aValue: Integer); begin TutlXmlHelper.SetAttribInt(fElement, aName, aValue); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlXmlHelper.SetAttribFloat(const aName: DOMString; const aValue: Double); begin TutlXmlHelper.SetAttribFloat(fElement, aName, aValue); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlXmlHelper.SetAttribBool(const aName: DOMString; const aValue: Boolean); begin TutlXmlHelper.SetAttribBool(fElement, aName, aValue); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TutlXmlHelper.GetAttribString(const aName: DOMString; const aDefault: String): String; begin result := TutlXmlHelper.GetAttribString(fElement, aName, aDefault); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TutlXmlHelper.GetAttribStringW(const aName: DOMString; const aDefault: WideString): WideString; begin result := TutlXmlHelper.GetAttribStringW(fElement, aName, aDefault); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TutlXmlHelper.GetAttribStringU(const aName: DOMString; const aDefault: UnicodeString): UnicodeString; begin result := TutlXmlHelper.GetAttribStringU(fElement, aName, aDefault); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TutlXmlHelper.GetAttribInt(const aName: DOMString; const aDefault: Int64): Int64; begin result := TutlXmlHelper.GetAttribInt(fElement, aName, aDefault); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TutlXmlHelper.GetAttribFloat(const aName: DOMString; const aDefault: Double): Double; begin result := TutlXmlHelper.GetAttribFloat(fElement, aName, aDefault); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TutlXmlHelper.GetAttribBool(const aName: DOMString; const aDefault: Boolean): Boolean; begin result := TutlXmlHelper.GetAttribBool(fElement, aName, aDefault); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TutlXmlHelper.TryGetAttribString(const aName: DOMString; out aValue: String): Boolean; begin result := TutlXmlHelper.TryGetAttribString(fElement, aName, aValue); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TutlXmlHelper.TryGetAttribStringW(const aName: DOMString; out aValue: WideString): Boolean; begin result := TutlXmlHelper.TryGetAttribStringW(fElement, aName, aValue); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TutlXmlHelper.TryGetAttribStringU(const aName: DOMString; out aValue: UnicodeString): Boolean; begin result := TutlXmlHelper.TryGetAttribStringU(fElement, aName, aValue); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TutlXmlHelper.TryGetAttribInt(const aName: DOMString; out aValue: Int64): Boolean; begin result := TutlXmlHelper.TryGetAttribInt(fElement, aName, aValue); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TutlXmlHelper.TryGetAttribFloat(const aName: DOMString; out aValue: Double): Boolean; begin result := TutlXmlHelper.TryGetAttribFloat(fElement, aName, aValue); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TutlXmlHelper.TryGetAttribBool(const aName: DOMString; out aValue: Boolean): Boolean; begin result := TutlXmlHelper.TryGetAttribBool(fElement, aName, aValue); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TutlXmlHelper.Nodes(const aName: DOMString): IutlNodeEnumerator; begin result := TutlXmlHelper.Nodes(fElement, aName); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TutlXmlHelper.Node(const aName: DOMString; const aCanCreate: Boolean): TDOMElement; begin result := TutlXmlHelper.Node(fElement, aName, aCanCreate); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TutlXmlHelper.PrependNode(const aName: DOMString): TDOMElement; begin result := TutlXmlHelper.PrependNode(fElement, aName); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TutlXmlHelper.AppendNode(const aName: DOMString): TDOMElement; begin result := TutlXmlHelper.AppendNode(fElement, aName); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlXmlHelper.PrependText(const aText: DOMString); begin TutlXmlHelper.PrependText(fElement, aText); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlXmlHelper.AppendText(const aText: DOMString); begin TutlXmlHelper.AppendText(fElement, aText); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// constructor TutlXmlHelper.Create; begin inherited Create; fElement := nil; end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// constructor TutlXmlHelper.Create(const aElement: TDOMElement); begin Create; fElement := aElement; end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class function TutlXmlHelper.Create(const aElement: TDOMElement): IutlXmlHelper; begin result := TutlXmlHelper.Create(aElement); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class function TutlXmlHelper.SetString(const aNode: TDOMNode; const aValue: String): TDOMNode; begin result := aNode; if Assigned(aNode) then aNode.TextContent := DOMString(aValue); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class function TutlXmlHelper.SetString(const aNode: TDOMNode; const aValue: WideString): TDOMNode; begin result := aNode; if Assigned(aNode) then aNode.TextContent := DOMString(aValue); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class function TutlXmlHelper.SetString(const aNode: TDOMNode; const aValue: UnicodeString): TDOMNode; begin result := aNode; if Assigned(aNode) then aNode.TextContent := DOMString(aValue); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class function TutlXmlHelper.SetInt(const aNode: TDOMNode; const aValue: Integer): TDOMNode; begin result := aNode; if Assigned(aNode) then aNode.TextContent := DOMString(IntToStr(aValue)); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class function TutlXmlHelper.SetFloat(const aNode: TDOMNode; const aValue: Double): TDOMNode; begin result := aNode; if Assigned(aNode) then aNode.TextContent := DOMString(FloatToStr(aValue)); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class function TutlXmlHelper.SetBool(const aNode: TDOMNode; const aValue: Boolean): TDOMNode; begin result := aNode; if Assigned(aNode) then begin if aValue then aNode.TextContent := 'true' else aNode.TextContent := 'false'; end; end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class function TutlXmlHelper.GetString(const aNode: TDOMNode; const aDefault: String): String; begin if not TryGetString(aNode, result) then result := aDefault; end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class function TutlXmlHelper.GetStringW(const aNode: TDOMNode; const aDefault: WideString): WideString; begin if not TryGetStringW(aNode, result) then result := aDefault; end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class function TutlXmlHelper.GetStringU(const aNode: TDOMNode; const aDefault: UnicodeString): UnicodeString; begin if not TryGetStringU(aNode, result) then result := aDefault; end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class function TutlXmlHelper.GetInt(const aNode: TDOMNode; const aDefault: Int64): Int64; begin if not TryGetInt(aNode, result) then result := aDefault; end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class function TutlXmlHelper.GetFloat(const aNode: TDOMNode; const aDefault: Double): Double; begin if not TryGetFloat(aNode, result) then result := aDefault; end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class function TutlXmlHelper.GetBool(const aNode: TDOMNode; const aDefault: Boolean): Boolean; begin if not TryGetBool(aNode, result) then result := aDefault; end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class function TutlXmlHelper.TryGetString(const aNode: TDOMNode; out aValue: String): Boolean; begin result := Assigned(aNode) and ( aNode.HasChildNodes or (aNode is TDOMText)); if result then aValue := String(aNode.TextContent); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class function TutlXmlHelper.TryGetStringW(const aNode: TDOMNode; out aValue: WideString): Boolean; begin result := Assigned(aNode) and ( aNode.HasChildNodes or (aNode is TDOMText)); if result then aValue := WideString(aNode.TextContent); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class function TutlXmlHelper.TryGetStringU(const aNode: TDOMNode; out aValue: UnicodeString): Boolean; begin result := Assigned(aNode) and ( aNode.HasChildNodes or (aNode is TDOMText)); if result then aValue := UnicodeString(aNode.TextContent); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class function TutlXmlHelper.TryGetInt(const aNode: TDOMNode; out aValue: Int64): Boolean; begin result := Assigned(aNode) and ( aNode.HasChildNodes or (aNode is TDOMText)) and TryStrToInt64(String(aNode.TextContent), aValue); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class function TutlXmlHelper.TryGetFloat(const aNode: TDOMNode; out aValue: Double): Boolean; begin result := Assigned(aNode) and ( aNode.HasChildNodes or (aNode is TDOMText)) and TryStrToFloat(String(aNode.TextContent), aValue); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class function TutlXmlHelper.TryGetBool(const aNode: TDOMNode; out aValue: Boolean): Boolean; var s: String; begin result := TryGetString(aNode, s); if result then begin if (s = 'true') or (s = 't') or (s = '1') then aValue := true else aValue := false; end; end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class procedure TutlXmlHelper.SetAttribString(const aNode: TDOMElement; const aName: DOMString; const aValue: String); begin aNode.SetAttributeNode(TutlXmlHelper.SetString(aNode.OwnerDocument.CreateAttribute(aName), aValue) as TDOMAttr); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class procedure TutlXmlHelper.SetAttribString(const aNode: TDOMElement; const aName: DOMString; const aValue: WideString); begin aNode.SetAttributeNode(TutlXmlHelper.SetString(aNode.OwnerDocument.CreateAttribute(aName), aValue) as TDOMAttr); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class procedure TutlXmlHelper.SetAttribString(const aNode: TDOMElement; const aName: DOMString; const aValue: UnicodeString); begin aNode.SetAttributeNode(TutlXmlHelper.SetString(aNode.OwnerDocument.CreateAttribute(aName), aValue) as TDOMAttr); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class procedure TutlXmlHelper.SetAttribInt(const aNode: TDOMElement; const aName: DOMString; const aValue: Integer); begin aNode.SetAttributeNode(TutlXmlHelper.SetInt(aNode.OwnerDocument.CreateAttribute(aName), aValue) as TDOMAttr); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class procedure TutlXmlHelper.SetAttribFloat(const aNode: TDOMElement; const aName: DOMString; const aValue: Double); begin aNode.SetAttributeNode(TutlXmlHelper.SetFloat(aNode.OwnerDocument.CreateAttribute(aName), aValue) as TDOMAttr); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class procedure TutlXmlHelper.SetAttribBool(const aNode: TDOMElement; const aName: DOMString; const aValue: Boolean); begin aNode.SetAttributeNode(TutlXmlHelper.SetBool(aNode.OwnerDocument.CreateAttribute(aName), aValue) as TDOMAttr); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class function TutlXmlHelper.GetAttribString(const aNode: TDOMElement; const aName: DOMString; const aDefault: String): String; begin result := TutlXmlHelper.GetString(aNode.Attributes.GetNamedItem(aName), aDefault); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class function TutlXmlHelper.GetAttribStringW(const aNode: TDOMElement; const aName: DOMString; const aDefault: WideString): WideString; begin result := TutlXmlHelper.GetStringW(aNode.Attributes.GetNamedItem(aName), aDefault); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class function TutlXmlHelper.GetAttribStringU(const aNode: TDOMElement; const aName: DOMString; const aDefault: UnicodeString): UnicodeString; begin result := TutlXmlHelper.GetStringU(aNode.Attributes.GetNamedItem(aName), aDefault); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class function TutlXmlHelper.GetAttribInt(const aNode: TDOMElement; const aName: DOMString; const aDefault: Int64): Int64; begin result := TutlXmlHelper.GetInt(aNode.Attributes.GetNamedItem(aName), aDefault); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class function TutlXmlHelper.GetAttribFloat(const aNode: TDOMElement; const aName: DOMString; const aDefault: Double): Double; begin result := TutlXmlHelper.GetFloat(aNode.Attributes.GetNamedItem(aName), aDefault); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class function TutlXmlHelper.GetAttribBool(const aNode: TDOMElement; const aName: DOMString; const aDefault: Boolean): Boolean; begin result := TutlXmlHelper.GetBool(aNode.Attributes.GetNamedItem(aName), aDefault); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class function TutlXmlHelper.TryGetAttribString(const aNode: TDOMElement; const aName: DOMString; out aValue: String): Boolean; begin result := TutlXmlHelper.TryGetString(aNode.Attributes.GetNamedItem(aName), aValue); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class function TutlXmlHelper.TryGetAttribStringW(const aNode: TDOMElement; const aName: DOMString; out aValue: WideString): Boolean; begin result := TutlXmlHelper.TryGetStringW(aNode.Attributes.GetNamedItem(aName), aValue); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class function TutlXmlHelper.TryGetAttribStringU(const aNode: TDOMElement; const aName: DOMString; out aValue: UnicodeString): Boolean; begin result := TutlXmlHelper.TryGetStringU(aNode.Attributes.GetNamedItem(aName), aValue); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class function TutlXmlHelper.TryGetAttribInt(const aNode: TDOMElement; const aName: DOMString; out aValue: Int64): Boolean; begin result := TutlXmlHelper.TryGetInt(aNode.Attributes.GetNamedItem(aName), aValue); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class function TutlXmlHelper.TryGetAttribFloat(const aNode: TDOMElement; const aName: DOMString; out aValue: Double): Boolean; begin result := TutlXmlHelper.TryGetFloat(aNode.Attributes.GetNamedItem(aName), aValue); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class function TutlXmlHelper.TryGetAttribBool(const aNode: TDOMElement; const aName: DOMString; out aValue: Boolean): Boolean; begin result := TutlXmlHelper.TryGetBool(aNode.Attributes.GetNamedItem(aName), aValue); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class function TutlXmlHelper.Nodes(const aElement: TDOMElement; const aName: DOMString): IutlNodeEnumerator; begin result := TutlNodeEnumerator.Create(aElement, aName); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class function TutlXmlHelper.Node(const aElement: TDOMElement; const aName: DOMString; const aCanCreate: Boolean): TDOMElement; var sl: TStringList; s: String; b: Boolean; i, c: Integer; begin sl := TStringList.Create; try sl.Delimiter := '.'; sl.DelimitedText := String(aName); result := aElement; for s in sl do begin if (s = '') then continue; if not Assigned(result) then exit; b := false; c := result.ChildNodes.Count; for i := 0 to c-1 do begin if (result.ChildNodes[i].NodeName = DOMString(s)) then begin result := TDOMElement(result.ChildNodes[i]); b := true; break; end; end; if not b then begin if aCanCreate then result := AppendNode(result, DOMString(s)) else result := nil; end; end; finally FreeAndNil(sl); end; end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class function TutlXmlHelper.PrependNode(const aElement: TDOMElement; const aName: DOMString): TDOMElement; begin result := aElement.OwnerDocument.CreateElement(aName); if aElement.HasChildNodes then aElement.InsertBefore(result, aElement.FirstChild) else aElement.AppendChild(result); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class function TutlXmlHelper.AppendNode(const aElement: TDOMElement; const aName: DOMString): TDOMElement; begin result := aElement.OwnerDocument.CreateElement(aName); aElement.AppendChild(result); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class procedure TutlXmlHelper.PrependText(const aElement: TDOMElement; const aText: DOMString); var n: TDOMNode; begin n := aElement.OwnerDocument.CreateTextNode(aText); if aElement.HasChildNodes then aElement.InsertBefore(n, aElement.FirstChild) else aElement.AppendChild(n); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class procedure TutlXmlHelper.AppendText(const aElement: TDOMElement; const aText: DOMString); begin aElement.AppendChild(aElement.OwnerDocument.CreateTextNode(aText)); end; end.