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 Node: 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): Int64; function GetFloat (const aDefault: Double): Double; function GetBool (const aDefault: 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); 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; // node operations function Nodes (const aName: DOMString): IutlNodeEnumerator; 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; // 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); 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; // node operations function Nodes (const aName: DOMString): IutlNodeEnumerator; 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 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; class function SetFloat (const aNode: TDOMNode; const aValue: Double): TDOMNode; class function SetBool (const aNode: TDOMNode; const aValue: Boolean): TDOMNode; 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; class function Nodes (const aElement: TDOMElement; const aName: DOMString = ''): IutlNodeEnumerator; 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; begin repeat inc(fIndex) until (fIndex {%H-}>= fParent.ChildNodes.Count) or ( (fName = '') or (fName = fParent.ChildNodes[fIndex].NodeName)); result := (fIndex {%H-}< fParent.ChildNodes.Count); 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; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlXmlHelper.SetAttribString(const aName: DOMString; const aValue: String); begin fElement.SetAttributeNode(TutlXmlHelper.SetString( fElement.OwnerDocument.CreateAttribute(aName), aValue) as TDOMAttr); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlXmlHelper.SetAttribString(const aName: DOMString; const aValue: WideString); begin fElement.SetAttributeNode(TutlXmlHelper.SetString( fElement.OwnerDocument.CreateAttribute(aName), aValue) as TDOMAttr); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlXmlHelper.SetAttribString(const aName: DOMString; const aValue: UnicodeString); begin fElement.SetAttributeNode(TutlXmlHelper.SetString( fElement.OwnerDocument.CreateAttribute(aName), aValue) as TDOMAttr); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlXmlHelper.SetAttribInt(const aName: DOMString; const aValue: Integer); begin fElement.SetAttributeNode(TutlXmlHelper.SetInt( fElement.OwnerDocument.CreateAttribute(aName), aValue) as TDOMAttr); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlXmlHelper.SetAttribFloat(const aName: DOMString; const aValue: Double); begin fElement.SetAttributeNode(TutlXmlHelper.SetFloat( fElement.OwnerDocument.CreateAttribute(aName), aValue) as TDOMAttr); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TutlXmlHelper.SetAttribBool(const aName: DOMString; const aValue: Boolean); begin fElement.SetAttributeNode(TutlXmlHelper.SetBool( fElement.OwnerDocument.CreateAttribute(aName), aValue) as TDOMAttr); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TutlXmlHelper.GetAttribString(const aName: DOMString; const aDefault: String): String; begin result := TutlXmlHelper.GetString(fElement.Attributes.GetNamedItem(aName), aDefault); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TutlXmlHelper.GetAttribStringW(const aName: DOMString; const aDefault: WideString): WideString; begin result := TutlXmlHelper.GetStringW(fElement.Attributes.GetNamedItem(aName), aDefault); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TutlXmlHelper.GetAttribStringU(const aName: DOMString; const aDefault: UnicodeString): UnicodeString; begin result := TutlXmlHelper.GetStringU(fElement.Attributes.GetNamedItem(aName), aDefault); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TutlXmlHelper.GetAttribInt(const aName: DOMString; const aDefault: Int64): Int64; begin result := TutlXmlHelper.GetInt(fElement.Attributes.GetNamedItem(aName), aDefault); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TutlXmlHelper.GetAttribFloat(const aName: DOMString; const aDefault: Double): Double; begin result := TutlXmlHelper.GetFloat(fElement.Attributes.GetNamedItem(aName), aDefault); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TutlXmlHelper.GetAttribBool(const aName: DOMString; const aDefault: Boolean): Boolean; begin result := TutlXmlHelper.GetBool(fElement.Attributes.GetNamedItem(aName), aDefault); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TutlXmlHelper.Nodes(const aName: DOMString): IutlNodeEnumerator; begin result := TutlXmlHelper.Nodes(fElement, aName); 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 Assigned(aNode) or ( not aNode.HasChildNodes and not (aNode is TDOMText)) then result := aDefault else result := String(aNode.TextContent); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class function TutlXmlHelper.GetStringW(const aNode: TDOMNode; const aDefault: WideString): WideString; begin if not Assigned(aNode) or ( not aNode.HasChildNodes and not (aNode is TDOMText)) then result := aDefault else result := WideString(aNode.TextContent); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class function TutlXmlHelper.GetStringU(const aNode: TDOMNode; const aDefault: UnicodeString): UnicodeString; begin if not Assigned(aNode) or ( not aNode.HasChildNodes and not (aNode is TDOMText)) then result := aDefault else result := UnicodeString(aNode.TextContent); end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class function TutlXmlHelper.GetInt(const aNode: TDOMNode; const aDefault: Int64): Int64; begin if not Assigned(aNode) or ( not aNode.HasChildNodes and not (aNode is TDOMText)) or not TryStrToInt64(String(aNode.TextContent), result) then result := aDefault; end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class function TutlXmlHelper.GetFloat(const aNode: TDOMNode; const aDefault: Double): Double; begin if not Assigned(aNode) or ( not aNode.HasChildNodes and not (aNode is TDOMText)) or not TryStrToFloat(String(aNode.TextContent), result) then result := aDefault; end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class function TutlXmlHelper.GetBool(const aNode: TDOMNode; const aDefault: Boolean): Boolean; var s: String; begin if not Assigned(aNode) or ( not aNode.HasChildNodes and not (aNode is TDOMText)) then begin result := aDefault; exit; end; s := LowerCase(String(aNode.TextContent)); if (s = 'true') or (s = 't') or (s = '1') then result := true else if (s = 'false') or (s = 'f') or (s = '0') then result := false else result := aDefault; end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class function TutlXmlHelper.Nodes(const aElement: TDOMElement; const aName: DOMString): IutlNodeEnumerator; begin result := TutlNodeEnumerator.Create(aElement, aName); 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.