Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

603 wiersze
28 KiB

  1. unit uutlXmlHelper;
  2. {$mode objfpc}{$H+}
  3. { *************************** EXAMPLE *********************************
  4. doc := TXMLDocument.Create;
  5. try
  6. root := doc.CreateElement('root');
  7. doc.AppendChild(root);
  8. with TutlXmlHelper.Create(root) do begin
  9. SetAttribString('test', 'blubb');
  10. SetAttribInt('new', 123);
  11. with TutlXmlHelper.Create(AppendNode('test')) do begin
  12. SetString('child node :)');
  13. end;
  14. with TutlXmlHelper.Create(AppendNode('test')) do begin
  15. SetString('another child node :)');
  16. end;
  17. end;
  18. WriteXMLFile(doc, 'save.xml');
  19. finally
  20. FreeAndNil(doc);
  21. end; }
  22. interface
  23. uses
  24. Classes, SysUtils, DOM;
  25. type
  26. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  27. IutlNodeEnumerator = interface(specialize IEnumerator<TDOMElement>)
  28. ['{59F90C67-8A0B-48AC-8A49-D39317A07FE2}']
  29. function GetEnumerator: IutlNodeEnumerator;
  30. end;
  31. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  32. IutlXmlHelper = interface(IUnknown)
  33. ['{C489CEC8-BAD6-4E03-A554-9DF31E84CDBB}']
  34. // simple properties
  35. function GetElement: TDOMElement;
  36. property Node: TDOMElement read GetElement;
  37. // set value of current node
  38. procedure SetString (const aValue: String); overload;
  39. procedure SetString (const aValue: WideString); overload;
  40. procedure SetString (const aValue: UnicodeString); overload;
  41. procedure SetInt (const aValue: Integer);
  42. procedure SetFloat (const aValue: Double);
  43. procedure SetBool (const aValue: Boolean);
  44. // get value of current node
  45. function GetString (const aDefault: String): String;
  46. function GetStringW (const aDefault: WideString): WideString;
  47. function GetStringU (const aDefault: UnicodeString): UnicodeString;
  48. function GetInt (const aDefault: Int64): Int64;
  49. function GetFloat (const aDefault: Double): Double;
  50. function GetBool (const aDefault: Boolean): Boolean;
  51. // set value of attribute
  52. procedure SetAttribString (const aName: DOMString; const aValue: String); overload;
  53. procedure SetAttribString (const aName: DOMString; const aValue: WideString); overload;
  54. procedure SetAttribString (const aName: DOMString; const aValue: UnicodeString); overload;
  55. procedure SetAttribInt (const aName: DOMString; const aValue: Integer);
  56. procedure SetAttribFloat (const aName: DOMString; const aValue: Double);
  57. procedure SetAttribBool (const aName: DOMString; const aValue: Boolean);
  58. // get value of attribute
  59. function GetAttribString (const aName: DOMString; const aDefault: String): String;
  60. function GetAttribStringW (const aName: DOMString; const aDefault: WideString): WideString;
  61. function GetAttribStringU (const aName: DOMString; const aDefault: UnicodeString): UnicodeString;
  62. function GetAttribInt (const aName: DOMString; const aDefault: Int64): Int64;
  63. function GetAttribFloat (const aName: DOMString; const aDefault: Double): Double;
  64. function GetAttribBool (const aName: DOMString; const aDefault: Boolean): Boolean;
  65. // node operations
  66. function Nodes (const aName: DOMString): IutlNodeEnumerator;
  67. function PrependNode (const aName: DOMString): TDOMElement;
  68. function AppendNode (const aName: DOMString): TDOMElement;
  69. procedure PrependText (const aText: DOMString);
  70. procedure AppendText (const aText: DOMString);
  71. end;
  72. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  73. TutlNodeEnumerator = class(TInterfacedObject, IutlNodeEnumerator)
  74. private
  75. fIndex: Integer;
  76. fName: DOMString;
  77. fParent: TDOMElement;
  78. public { INodeEnumerator }
  79. function GetEnumerator: IutlNodeEnumerator;
  80. function GetCurrent: TDOMElement;
  81. function MoveNext: Boolean;
  82. procedure Reset;
  83. public
  84. constructor Create(const aParent: TDOMElement; const aName: DOMString);
  85. end;
  86. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  87. TutlXmlHelper = class(TInterfacedObject, IutlXmlHelper)
  88. private
  89. fElement: TDOMElement;
  90. public { IutlXmlHelper }
  91. function GetElement: TDOMElement;
  92. // set value of current node
  93. procedure SetString (const aValue: String); overload;
  94. procedure SetString (const aValue: WideString); overload;
  95. procedure SetString (const aValue: UnicodeString); overload;
  96. procedure SetInt (const aValue: Integer);
  97. procedure SetFloat (const aValue: Double);
  98. procedure SetBool (const aValue: Boolean);
  99. // get value of current node
  100. function GetString (const aDefault: String): String;
  101. function GetStringW (const aDefault: WideString): WideString;
  102. function GetStringU (const aDefault: UnicodeString): UnicodeString;
  103. function GetInt (const aDefault: Int64): Int64;
  104. function GetFloat (const aDefault: Double): Double;
  105. function GetBool (const aDefault: Boolean): Boolean;
  106. // set value of attribute
  107. procedure SetAttribString (const aName: DOMString; const aValue: String); overload;
  108. procedure SetAttribString (const aName: DOMString; const aValue: WideString); overload;
  109. procedure SetAttribString (const aName: DOMString; const aValue: UnicodeString); overload;
  110. procedure SetAttribInt (const aName: DOMString; const aValue: Integer);
  111. procedure SetAttribFloat (const aName: DOMString; const aValue: Double);
  112. procedure SetAttribBool (const aName: DOMString; const aValue: Boolean);
  113. // get value of attribute
  114. function GetAttribString (const aName: DOMString; const aDefault: String): String;
  115. function GetAttribStringW (const aName: DOMString; const aDefault: WideString): WideString;
  116. function GetAttribStringU (const aName: DOMString; const aDefault: UnicodeString): UnicodeString;
  117. function GetAttribInt (const aName: DOMString; const aDefault: Int64): Int64;
  118. function GetAttribFloat (const aName: DOMString; const aDefault: Double): Double;
  119. function GetAttribBool (const aName: DOMString; const aDefault: Boolean): Boolean;
  120. // node operations
  121. function Nodes (const aName: DOMString): IutlNodeEnumerator;
  122. function PrependNode (const aName: DOMString): TDOMElement;
  123. function AppendNode (const aName: DOMString): TDOMElement;
  124. procedure PrependText (const aText: DOMString);
  125. procedure AppendText (const aText: DOMString);
  126. private
  127. {%H-}constructor Create; reintroduce;
  128. {%H-}constructor Create(const aElement: TDOMElement); reintroduce;
  129. public
  130. class function Create(const aElement: TDOMElement): IutlXmlHelper;
  131. end;
  132. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  133. TutlXmlHelperImpl = class
  134. public
  135. class function SetString (const aNode: TDOMNode; const aValue: String): TDOMNode; overload;
  136. class function SetString (const aNode: TDOMNode; const aValue: WideString): TDOMNode; overload;
  137. class function SetString (const aNode: TDOMNode; const aValue: UnicodeString): TDOMNode; overload;
  138. class function SetInt (const aNode: TDOMNode; const aValue: Integer): TDOMNode;
  139. class function SetFloat (const aNode: TDOMNode; const aValue: Double): TDOMNode;
  140. class function SetBool (const aNode: TDOMNode; const aValue: Boolean): TDOMNode;
  141. class function GetString (const aNode: TDOMNode; const aDefault: String): String;
  142. class function GetStringW (const aNode: TDOMNode; const aDefault: WideString): WideString;
  143. class function GetStringU (const aNode: TDOMNode; const aDefault: UnicodeString): UnicodeString;
  144. class function GetInt (const aNode: TDOMNode; const aDefault: Int64): Int64;
  145. class function GetFloat (const aNode: TDOMNode; const aDefault: Double): Double;
  146. class function GetBool (const aNode: TDOMNode; const aDefault: Boolean): Boolean;
  147. class function Nodes (const aElement: TDOMElement; const aName: DOMString = ''): IutlNodeEnumerator;
  148. class function PrependNode (const aElement: TDOMElement; const aName: DOMString): TDOMElement;
  149. class function AppendNode (const aElement: TDOMElement; const aName: DOMString): TDOMElement;
  150. class procedure PrependText (const aElement: TDOMElement; const aText: DOMString);
  151. class procedure AppendText (const aElement: TDOMElement; const aText: DOMString);
  152. end;
  153. implementation
  154. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  155. //TutlNodeEnumerator/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  156. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  157. function TutlNodeEnumerator.GetEnumerator: IutlNodeEnumerator;
  158. begin
  159. result := self;
  160. end;
  161. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  162. function TutlNodeEnumerator.GetCurrent: TDOMElement;
  163. begin
  164. result := (fParent.ChildNodes[fIndex] as TDOMElement);
  165. end;
  166. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  167. function TutlNodeEnumerator.MoveNext: Boolean;
  168. begin
  169. repeat
  170. inc(fIndex)
  171. until (fIndex >= fParent.ChildNodes.Count)
  172. or ( (fName = '')
  173. or (fName = fParent.ChildNodes[fIndex].NodeName));
  174. result := (fIndex < fParent.ChildNodes.Count);
  175. end;
  176. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  177. procedure TutlNodeEnumerator.Reset;
  178. begin
  179. fIndex := -1;
  180. end;
  181. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  182. constructor TutlNodeEnumerator.Create(const aParent: TDOMElement; const aName: DOMString);
  183. begin
  184. inherited Create;
  185. fParent := aParent;
  186. fName := aName;
  187. fIndex := -1;
  188. end;
  189. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  190. //TutlXmlHelper//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  191. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  192. function TutlXmlHelper.GetElement: TDOMElement;
  193. begin
  194. result := fElement;
  195. end;
  196. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  197. procedure TutlXmlHelper.SetString(const aValue: String);
  198. begin
  199. TutlXmlHelperImpl.SetString(fElement, aValue);
  200. end;
  201. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  202. procedure TutlXmlHelper.SetString(const aValue: WideString);
  203. begin
  204. TutlXmlHelperImpl.SetString(fElement, aValue);
  205. end;
  206. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  207. procedure TutlXmlHelper.SetString(const aValue: UnicodeString);
  208. begin
  209. TutlXmlHelperImpl.SetString(fElement, aValue);
  210. end;
  211. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  212. procedure TutlXmlHelper.SetInt(const aValue: Integer);
  213. begin
  214. TutlXmlHelperImpl.SetInt(fElement, aValue);
  215. end;
  216. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  217. procedure TutlXmlHelper.SetFloat(const aValue: Double);
  218. begin
  219. TutlXmlHelperImpl.SetFloat(fElement, aValue);
  220. end;
  221. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  222. procedure TutlXmlHelper.SetBool(const aValue: Boolean);
  223. begin
  224. TutlXmlHelperImpl.SetBool(fElement, aValue);
  225. end;
  226. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  227. function TutlXmlHelper.GetString(const aDefault: String): String;
  228. begin
  229. result := TutlXmlHelperImpl.GetString(fElement, aDefault);
  230. end;
  231. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  232. function TutlXmlHelper.GetStringW(const aDefault: WideString): WideString;
  233. begin
  234. result := TutlXmlHelperImpl.GetStringW(fElement, aDefault);
  235. end;
  236. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  237. function TutlXmlHelper.GetStringU(const aDefault: UnicodeString): UnicodeString;
  238. begin
  239. result := TutlXmlHelperImpl.GetStringU(fElement, aDefault);
  240. end;
  241. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  242. function TutlXmlHelper.GetInt(const aDefault: Int64): Int64;
  243. begin
  244. result := TutlXmlHelperImpl.GetInt(fElement, aDefault);
  245. end;
  246. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  247. function TutlXmlHelper.GetFloat(const aDefault: Double): Double;
  248. begin
  249. result := TutlXmlHelperImpl.GetFloat(fElement, aDefault);
  250. end;
  251. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  252. function TutlXmlHelper.GetBool(const aDefault: Boolean): Boolean;
  253. begin
  254. result := TutlXmlHelperImpl.GetBool(fElement, aDefault);
  255. end;
  256. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  257. procedure TutlXmlHelper.SetAttribString(const aName: DOMString; const aValue: String);
  258. begin
  259. fElement.SetAttributeNode(TutlXmlHelperImpl.SetString(
  260. fElement.OwnerDocument.CreateAttribute(aName),
  261. aValue) as TDOMAttr);
  262. end;
  263. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  264. procedure TutlXmlHelper.SetAttribString(const aName: DOMString; const aValue: WideString);
  265. begin
  266. fElement.SetAttributeNode(TutlXmlHelperImpl.SetString(
  267. fElement.OwnerDocument.CreateAttribute(aName),
  268. aValue) as TDOMAttr);
  269. end;
  270. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  271. procedure TutlXmlHelper.SetAttribString(const aName: DOMString; const aValue: UnicodeString);
  272. begin
  273. fElement.SetAttributeNode(TutlXmlHelperImpl.SetString(
  274. fElement.OwnerDocument.CreateAttribute(aName),
  275. aValue) as TDOMAttr);
  276. end;
  277. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  278. procedure TutlXmlHelper.SetAttribInt(const aName: DOMString; const aValue: Integer);
  279. begin
  280. fElement.SetAttributeNode(TutlXmlHelperImpl.SetInt(
  281. fElement.OwnerDocument.CreateAttribute(aName),
  282. aValue) as TDOMAttr);
  283. end;
  284. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  285. procedure TutlXmlHelper.SetAttribFloat(const aName: DOMString; const aValue: Double);
  286. begin
  287. fElement.SetAttributeNode(TutlXmlHelperImpl.SetFloat(
  288. fElement.OwnerDocument.CreateAttribute(aName),
  289. aValue) as TDOMAttr);
  290. end;
  291. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  292. procedure TutlXmlHelper.SetAttribBool(const aName: DOMString; const aValue: Boolean);
  293. begin
  294. fElement.SetAttributeNode(TutlXmlHelperImpl.SetBool(
  295. fElement.OwnerDocument.CreateAttribute(aName),
  296. aValue) as TDOMAttr);
  297. end;
  298. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  299. function TutlXmlHelper.GetAttribString(const aName: DOMString; const aDefault: String): String;
  300. begin
  301. result := TutlXmlHelperImpl.GetString(fElement.Attributes.GetNamedItem(aName), aDefault);
  302. end;
  303. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  304. function TutlXmlHelper.GetAttribStringW(const aName: DOMString; const aDefault: WideString): WideString;
  305. begin
  306. result := TutlXmlHelperImpl.GetStringW(fElement.Attributes.GetNamedItem(aName), aDefault);
  307. end;
  308. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  309. function TutlXmlHelper.GetAttribStringU(const aName: DOMString; const aDefault: UnicodeString): UnicodeString;
  310. begin
  311. result := TutlXmlHelperImpl.GetStringU(fElement.Attributes.GetNamedItem(aName), aDefault);
  312. end;
  313. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  314. function TutlXmlHelper.GetAttribInt(const aName: DOMString; const aDefault: Int64): Int64;
  315. begin
  316. result := TutlXmlHelperImpl.GetInt(fElement.Attributes.GetNamedItem(aName), aDefault);
  317. end;
  318. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  319. function TutlXmlHelper.GetAttribFloat(const aName: DOMString; const aDefault: Double): Double;
  320. begin
  321. result := TutlXmlHelperImpl.GetFloat(fElement.Attributes.GetNamedItem(aName), aDefault);
  322. end;
  323. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  324. function TutlXmlHelper.GetAttribBool(const aName: DOMString; const aDefault: Boolean): Boolean;
  325. begin
  326. result := TutlXmlHelperImpl.GetBool(fElement.Attributes.GetNamedItem(aName), aDefault);
  327. end;
  328. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  329. function TutlXmlHelper.Nodes(const aName: DOMString): IutlNodeEnumerator;
  330. begin
  331. result := TutlXmlHelperImpl.Nodes(fElement, aName);
  332. end;
  333. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  334. function TutlXmlHelper.PrependNode(const aName: DOMString): TDOMElement;
  335. begin
  336. result := TutlXmlHelperImpl.PrependNode(fElement, aName);
  337. end;
  338. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  339. function TutlXmlHelper.AppendNode(const aName: DOMString): TDOMElement;
  340. begin
  341. result := TutlXmlHelperImpl.AppendNode(fElement, aName);
  342. end;
  343. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  344. procedure TutlXmlHelper.PrependText(const aText: DOMString);
  345. begin
  346. TutlXmlHelperImpl.PrependText(fElement, aText);
  347. end;
  348. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  349. procedure TutlXmlHelper.AppendText(const aText: DOMString);
  350. begin
  351. TutlXmlHelperImpl.AppendText(fElement, aText);
  352. end;
  353. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  354. constructor TutlXmlHelper.Create;
  355. begin
  356. inherited Create;
  357. fElement := nil;
  358. end;
  359. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  360. constructor TutlXmlHelper.Create(const aElement: TDOMElement);
  361. begin
  362. Create;
  363. fElement := aElement;
  364. end;
  365. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  366. class function TutlXmlHelper.Create(const aElement: TDOMElement): IutlXmlHelper;
  367. begin
  368. result := TutlXmlHelper.Create(aElement);
  369. end;
  370. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  371. //TutlXmlHelperImpl//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  372. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  373. class function TutlXmlHelperImpl.SetString(const aNode: TDOMNode; const aValue: String): TDOMNode;
  374. begin
  375. result := aNode;
  376. if Assigned(aNode) then
  377. aNode.TextContent := DOMString(aValue);
  378. end;
  379. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  380. class function TutlXmlHelperImpl.SetString(const aNode: TDOMNode; const aValue: WideString): TDOMNode;
  381. begin
  382. result := aNode;
  383. if Assigned(aNode) then
  384. aNode.TextContent := DOMString(aValue);
  385. end;
  386. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  387. class function TutlXmlHelperImpl.SetString(const aNode: TDOMNode; const aValue: UnicodeString): TDOMNode;
  388. begin
  389. result := aNode;
  390. if Assigned(aNode) then
  391. aNode.TextContent := DOMString(aValue);
  392. end;
  393. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  394. class function TutlXmlHelperImpl.SetInt(const aNode: TDOMNode; const aValue: Integer): TDOMNode;
  395. begin
  396. result := aNode;
  397. if Assigned(aNode) then
  398. aNode.TextContent := DOMString(IntToStr(aValue));
  399. end;
  400. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  401. class function TutlXmlHelperImpl.SetFloat(const aNode: TDOMNode; const aValue: Double): TDOMNode;
  402. begin
  403. result := aNode;
  404. if Assigned(aNode) then
  405. aNode.TextContent := DOMString(FloatToStr(aValue));
  406. end;
  407. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  408. class function TutlXmlHelperImpl.SetBool(const aNode: TDOMNode; const aValue: Boolean): TDOMNode;
  409. begin
  410. result := aNode;
  411. if Assigned(aNode) then begin
  412. if aValue
  413. then aNode.TextContent := 'true'
  414. else aNode.TextContent := 'false';
  415. end;
  416. end;
  417. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  418. class function TutlXmlHelperImpl.GetString(const aNode: TDOMNode; const aDefault: String): String;
  419. begin
  420. if not Assigned(aNode)
  421. or ( not aNode.HasChildNodes
  422. and not (aNode is TDOMText))
  423. then result := aDefault
  424. else result := String(aNode.TextContent);
  425. end;
  426. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  427. class function TutlXmlHelperImpl.GetStringW(const aNode: TDOMNode; const aDefault: WideString): WideString;
  428. begin
  429. if not Assigned(aNode)
  430. or ( not aNode.HasChildNodes
  431. and not (aNode is TDOMText))
  432. then result := aDefault
  433. else result := WideString(aNode.TextContent);
  434. end;
  435. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  436. class function TutlXmlHelperImpl.GetStringU(const aNode: TDOMNode; const aDefault: UnicodeString): UnicodeString;
  437. begin
  438. if not Assigned(aNode)
  439. or ( not aNode.HasChildNodes
  440. and not (aNode is TDOMText))
  441. then result := aDefault
  442. else result := UnicodeString(aNode.TextContent);
  443. end;
  444. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  445. class function TutlXmlHelperImpl.GetInt(const aNode: TDOMNode; const aDefault: Int64): Int64;
  446. begin
  447. if not Assigned(aNode)
  448. or ( not aNode.HasChildNodes
  449. and not (aNode is TDOMText))
  450. or not TryStrToInt64(String(aNode.TextContent), result) then
  451. result := aDefault;
  452. end;
  453. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  454. class function TutlXmlHelperImpl.GetFloat(const aNode: TDOMNode; const aDefault: Double): Double;
  455. begin
  456. if not Assigned(aNode)
  457. or ( not aNode.HasChildNodes
  458. and not (aNode is TDOMText))
  459. or not TryStrToFloat(String(aNode.TextContent), result) then
  460. result := aDefault;
  461. end;
  462. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  463. class function TutlXmlHelperImpl.GetBool(const aNode: TDOMNode; const aDefault: Boolean): Boolean;
  464. var
  465. s: String;
  466. begin
  467. if not Assigned(aNode)
  468. or ( not aNode.HasChildNodes
  469. and not (aNode is TDOMText))
  470. then
  471. begin
  472. result := aDefault;
  473. exit;
  474. end;
  475. s := LowerCase(String(aNode.TextContent));
  476. if (s = 'true') or (s = 't') or (s = '1') then
  477. result := true
  478. else if (s = 'false') or (s = 'f') or (s = '0') then
  479. result := false
  480. else
  481. result := aDefault;
  482. end;
  483. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  484. class function TutlXmlHelperImpl.Nodes(const aElement: TDOMElement; const aName: DOMString): IutlNodeEnumerator;
  485. begin
  486. result := TutlNodeEnumerator.Create(aElement, aName);
  487. end;
  488. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  489. class function TutlXmlHelperImpl.PrependNode(const aElement: TDOMElement; const aName: DOMString): TDOMElement;
  490. begin
  491. result := aElement.OwnerDocument.CreateElement(aName);
  492. if aElement.HasChildNodes
  493. then aElement.InsertBefore(result, aElement.FirstChild)
  494. else aElement.AppendChild(result);
  495. end;
  496. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  497. class function TutlXmlHelperImpl.AppendNode(const aElement: TDOMElement; const aName: DOMString): TDOMElement;
  498. begin
  499. result := aElement.OwnerDocument.CreateElement(aName);
  500. aElement.AppendChild(result);
  501. end;
  502. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  503. class procedure TutlXmlHelperImpl.PrependText(const aElement: TDOMElement; const aText: DOMString);
  504. var n: TDOMNode;
  505. begin
  506. n := aElement.OwnerDocument.CreateTextNode(aText);
  507. if aElement.HasChildNodes
  508. then aElement.InsertBefore(n, aElement.FirstChild)
  509. else aElement.AppendChild(n);
  510. end;
  511. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  512. class procedure TutlXmlHelperImpl.AppendText(const aElement: TDOMElement; const aText: DOMString);
  513. begin
  514. aElement.AppendChild(aElement.OwnerDocument.CreateTextNode(aText));
  515. end;
  516. end.