| @@ -0,0 +1,602 @@ | |||
| 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<TDOMElement>) | |||
| ['{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; | |||
| end; | |||
| ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |||
| TutlXmlHelperImpl = class | |||
| 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 >= fParent.ChildNodes.Count) | |||
| or ( (fName = '') | |||
| or (fName = fParent.ChildNodes[fIndex].NodeName)); | |||
| result := (fIndex < 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 | |||
| TutlXmlHelperImpl.SetString(fElement, aValue); | |||
| end; | |||
| ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |||
| procedure TutlXmlHelper.SetString(const aValue: WideString); | |||
| begin | |||
| TutlXmlHelperImpl.SetString(fElement, aValue); | |||
| end; | |||
| ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |||
| procedure TutlXmlHelper.SetString(const aValue: UnicodeString); | |||
| begin | |||
| TutlXmlHelperImpl.SetString(fElement, aValue); | |||
| end; | |||
| ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |||
| procedure TutlXmlHelper.SetInt(const aValue: Integer); | |||
| begin | |||
| TutlXmlHelperImpl.SetInt(fElement, aValue); | |||
| end; | |||
| ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |||
| procedure TutlXmlHelper.SetFloat(const aValue: Double); | |||
| begin | |||
| TutlXmlHelperImpl.SetFloat(fElement, aValue); | |||
| end; | |||
| ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |||
| procedure TutlXmlHelper.SetBool(const aValue: Boolean); | |||
| begin | |||
| TutlXmlHelperImpl.SetBool(fElement, aValue); | |||
| end; | |||
| ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |||
| function TutlXmlHelper.GetString(const aDefault: String): String; | |||
| begin | |||
| result := TutlXmlHelperImpl.GetString(fElement, aDefault); | |||
| end; | |||
| ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |||
| function TutlXmlHelper.GetStringW(const aDefault: WideString): WideString; | |||
| begin | |||
| result := TutlXmlHelperImpl.GetStringW(fElement, aDefault); | |||
| end; | |||
| ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |||
| function TutlXmlHelper.GetStringU(const aDefault: UnicodeString): UnicodeString; | |||
| begin | |||
| result := TutlXmlHelperImpl.GetStringU(fElement, aDefault); | |||
| end; | |||
| ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |||
| function TutlXmlHelper.GetInt(const aDefault: Int64): Int64; | |||
| begin | |||
| result := TutlXmlHelperImpl.GetInt(fElement, aDefault); | |||
| end; | |||
| ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |||
| function TutlXmlHelper.GetFloat(const aDefault: Double): Double; | |||
| begin | |||
| result := TutlXmlHelperImpl.GetFloat(fElement, aDefault); | |||
| end; | |||
| ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |||
| function TutlXmlHelper.GetBool(const aDefault: Boolean): Boolean; | |||
| begin | |||
| result := TutlXmlHelperImpl.GetBool(fElement, aDefault); | |||
| end; | |||
| ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |||
| procedure TutlXmlHelper.SetAttribString(const aName: DOMString; const aValue: String); | |||
| begin | |||
| fElement.SetAttributeNode(TutlXmlHelperImpl.SetString( | |||
| fElement.OwnerDocument.CreateAttribute(aName), | |||
| aValue) as TDOMAttr); | |||
| end; | |||
| ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |||
| procedure TutlXmlHelper.SetAttribString(const aName: DOMString; const aValue: WideString); | |||
| begin | |||
| fElement.SetAttributeNode(TutlXmlHelperImpl.SetString( | |||
| fElement.OwnerDocument.CreateAttribute(aName), | |||
| aValue) as TDOMAttr); | |||
| end; | |||
| ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |||
| procedure TutlXmlHelper.SetAttribString(const aName: DOMString; const aValue: UnicodeString); | |||
| begin | |||
| fElement.SetAttributeNode(TutlXmlHelperImpl.SetString( | |||
| fElement.OwnerDocument.CreateAttribute(aName), | |||
| aValue) as TDOMAttr); | |||
| end; | |||
| ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |||
| procedure TutlXmlHelper.SetAttribInt(const aName: DOMString; const aValue: Integer); | |||
| begin | |||
| fElement.SetAttributeNode(TutlXmlHelperImpl.SetInt( | |||
| fElement.OwnerDocument.CreateAttribute(aName), | |||
| aValue) as TDOMAttr); | |||
| end; | |||
| ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |||
| procedure TutlXmlHelper.SetAttribFloat(const aName: DOMString; const aValue: Double); | |||
| begin | |||
| fElement.SetAttributeNode(TutlXmlHelperImpl.SetFloat( | |||
| fElement.OwnerDocument.CreateAttribute(aName), | |||
| aValue) as TDOMAttr); | |||
| end; | |||
| ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |||
| procedure TutlXmlHelper.SetAttribBool(const aName: DOMString; const aValue: Boolean); | |||
| begin | |||
| fElement.SetAttributeNode(TutlXmlHelperImpl.SetBool( | |||
| fElement.OwnerDocument.CreateAttribute(aName), | |||
| aValue) as TDOMAttr); | |||
| end; | |||
| ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |||
| function TutlXmlHelper.GetAttribString(const aName: DOMString; const aDefault: String): String; | |||
| begin | |||
| result := TutlXmlHelperImpl.GetString(fElement.Attributes.GetNamedItem(aName), aDefault); | |||
| end; | |||
| ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |||
| function TutlXmlHelper.GetAttribStringW(const aName: DOMString; const aDefault: WideString): WideString; | |||
| begin | |||
| result := TutlXmlHelperImpl.GetStringW(fElement.Attributes.GetNamedItem(aName), aDefault); | |||
| end; | |||
| ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |||
| function TutlXmlHelper.GetAttribStringU(const aName: DOMString; const aDefault: UnicodeString): UnicodeString; | |||
| begin | |||
| result := TutlXmlHelperImpl.GetStringU(fElement.Attributes.GetNamedItem(aName), aDefault); | |||
| end; | |||
| ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |||
| function TutlXmlHelper.GetAttribInt(const aName: DOMString; const aDefault: Int64): Int64; | |||
| begin | |||
| result := TutlXmlHelperImpl.GetInt(fElement.Attributes.GetNamedItem(aName), aDefault); | |||
| end; | |||
| ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |||
| function TutlXmlHelper.GetAttribFloat(const aName: DOMString; const aDefault: Double): Double; | |||
| begin | |||
| result := TutlXmlHelperImpl.GetFloat(fElement.Attributes.GetNamedItem(aName), aDefault); | |||
| end; | |||
| ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |||
| function TutlXmlHelper.GetAttribBool(const aName: DOMString; const aDefault: Boolean): Boolean; | |||
| begin | |||
| result := TutlXmlHelperImpl.GetBool(fElement.Attributes.GetNamedItem(aName), aDefault); | |||
| end; | |||
| ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |||
| function TutlXmlHelper.Nodes(const aName: DOMString): IutlNodeEnumerator; | |||
| begin | |||
| result := TutlXmlHelperImpl.Nodes(fElement, aName); | |||
| end; | |||
| ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |||
| function TutlXmlHelper.PrependNode(const aName: DOMString): TDOMElement; | |||
| begin | |||
| result := TutlXmlHelperImpl.PrependNode(fElement, aName); | |||
| end; | |||
| ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |||
| function TutlXmlHelper.AppendNode(const aName: DOMString): TDOMElement; | |||
| begin | |||
| result := TutlXmlHelperImpl.AppendNode(fElement, aName); | |||
| end; | |||
| ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |||
| procedure TutlXmlHelper.PrependText(const aText: DOMString); | |||
| begin | |||
| TutlXmlHelperImpl.PrependText(fElement, aText); | |||
| end; | |||
| ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |||
| procedure TutlXmlHelper.AppendText(const aText: DOMString); | |||
| begin | |||
| TutlXmlHelperImpl.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; | |||
| ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |||
| //TutlXmlHelperImpl////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |||
| ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |||
| class function TutlXmlHelperImpl.SetString(const aNode: TDOMNode; const aValue: String): TDOMNode; | |||
| begin | |||
| result := aNode; | |||
| if Assigned(aNode) then | |||
| aNode.TextContent := DOMString(aValue); | |||
| end; | |||
| ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |||
| class function TutlXmlHelperImpl.SetString(const aNode: TDOMNode; const aValue: WideString): TDOMNode; | |||
| begin | |||
| result := aNode; | |||
| if Assigned(aNode) then | |||
| aNode.TextContent := DOMString(aValue); | |||
| end; | |||
| ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |||
| class function TutlXmlHelperImpl.SetString(const aNode: TDOMNode; const aValue: UnicodeString): TDOMNode; | |||
| begin | |||
| result := aNode; | |||
| if Assigned(aNode) then | |||
| aNode.TextContent := DOMString(aValue); | |||
| end; | |||
| ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |||
| class function TutlXmlHelperImpl.SetInt(const aNode: TDOMNode; const aValue: Integer): TDOMNode; | |||
| begin | |||
| result := aNode; | |||
| if Assigned(aNode) then | |||
| aNode.TextContent := DOMString(IntToStr(aValue)); | |||
| end; | |||
| ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |||
| class function TutlXmlHelperImpl.SetFloat(const aNode: TDOMNode; const aValue: Double): TDOMNode; | |||
| begin | |||
| result := aNode; | |||
| if Assigned(aNode) then | |||
| aNode.TextContent := DOMString(FloatToStr(aValue)); | |||
| end; | |||
| ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |||
| class function TutlXmlHelperImpl.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 TutlXmlHelperImpl.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 TutlXmlHelperImpl.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 TutlXmlHelperImpl.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 TutlXmlHelperImpl.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 TutlXmlHelperImpl.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 TutlXmlHelperImpl.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 TutlXmlHelperImpl.Nodes(const aElement: TDOMElement; const aName: DOMString): IutlNodeEnumerator; | |||
| begin | |||
| result := TutlNodeEnumerator.Create(aElement, aName); | |||
| end; | |||
| ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |||
| class function TutlXmlHelperImpl.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 TutlXmlHelperImpl.AppendNode(const aElement: TDOMElement; const aName: DOMString): TDOMElement; | |||
| begin | |||
| result := aElement.OwnerDocument.CreateElement(aName); | |||
| aElement.AppendChild(result); | |||
| end; | |||
| ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |||
| class procedure TutlXmlHelperImpl.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 TutlXmlHelperImpl.AppendText(const aElement: TDOMElement; const aText: DOMString); | |||
| begin | |||
| aElement.AppendChild(aElement.OwnerDocument.CreateTextNode(aText)); | |||
| end; | |||
| end. | |||