Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

598 строки
27 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. public
  132. class function SetString (const aNode: TDOMNode; const aValue: String): TDOMNode; overload;
  133. class function SetString (const aNode: TDOMNode; const aValue: WideString): TDOMNode; overload;
  134. class function SetString (const aNode: TDOMNode; const aValue: UnicodeString): TDOMNode; overload;
  135. class function SetInt (const aNode: TDOMNode; const aValue: Integer): TDOMNode;
  136. class function SetFloat (const aNode: TDOMNode; const aValue: Double): TDOMNode;
  137. class function SetBool (const aNode: TDOMNode; const aValue: Boolean): TDOMNode;
  138. class function GetString (const aNode: TDOMNode; const aDefault: String): String;
  139. class function GetStringW (const aNode: TDOMNode; const aDefault: WideString): WideString;
  140. class function GetStringU (const aNode: TDOMNode; const aDefault: UnicodeString): UnicodeString;
  141. class function GetInt (const aNode: TDOMNode; const aDefault: Int64): Int64;
  142. class function GetFloat (const aNode: TDOMNode; const aDefault: Double): Double;
  143. class function GetBool (const aNode: TDOMNode; const aDefault: Boolean): Boolean;
  144. class function Nodes (const aElement: TDOMElement; const aName: DOMString = ''): IutlNodeEnumerator;
  145. class function PrependNode (const aElement: TDOMElement; const aName: DOMString): TDOMElement;
  146. class function AppendNode (const aElement: TDOMElement; const aName: DOMString): TDOMElement;
  147. class procedure PrependText (const aElement: TDOMElement; const aText: DOMString);
  148. class procedure AppendText (const aElement: TDOMElement; const aText: DOMString);
  149. end;
  150. implementation
  151. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  152. //TutlNodeEnumerator/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  153. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  154. function TutlNodeEnumerator.GetEnumerator: IutlNodeEnumerator;
  155. begin
  156. result := self;
  157. end;
  158. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  159. function TutlNodeEnumerator.GetCurrent: TDOMElement;
  160. begin
  161. result := (fParent.ChildNodes[fIndex] as TDOMElement);
  162. end;
  163. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  164. function TutlNodeEnumerator.MoveNext: Boolean;
  165. begin
  166. repeat
  167. inc(fIndex)
  168. until (fIndex {%H-}>= fParent.ChildNodes.Count)
  169. or ( (fName = '')
  170. or (fName = fParent.ChildNodes[fIndex].NodeName));
  171. result := (fIndex {%H-}< fParent.ChildNodes.Count);
  172. end;
  173. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  174. procedure TutlNodeEnumerator.Reset;
  175. begin
  176. fIndex := -1;
  177. end;
  178. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  179. constructor TutlNodeEnumerator.Create(const aParent: TDOMElement; const aName: DOMString);
  180. begin
  181. inherited Create;
  182. fParent := aParent;
  183. fName := aName;
  184. fIndex := -1;
  185. end;
  186. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  187. //TutlXmlHelper//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  188. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  189. function TutlXmlHelper.GetElement: TDOMElement;
  190. begin
  191. result := fElement;
  192. end;
  193. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  194. procedure TutlXmlHelper.SetString(const aValue: String);
  195. begin
  196. TutlXmlHelper.SetString(fElement, aValue);
  197. end;
  198. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  199. procedure TutlXmlHelper.SetString(const aValue: WideString);
  200. begin
  201. TutlXmlHelper.SetString(fElement, aValue);
  202. end;
  203. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  204. procedure TutlXmlHelper.SetString(const aValue: UnicodeString);
  205. begin
  206. TutlXmlHelper.SetString(fElement, aValue);
  207. end;
  208. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  209. procedure TutlXmlHelper.SetInt(const aValue: Integer);
  210. begin
  211. TutlXmlHelper.SetInt(fElement, aValue);
  212. end;
  213. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  214. procedure TutlXmlHelper.SetFloat(const aValue: Double);
  215. begin
  216. TutlXmlHelper.SetFloat(fElement, aValue);
  217. end;
  218. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  219. procedure TutlXmlHelper.SetBool(const aValue: Boolean);
  220. begin
  221. TutlXmlHelper.SetBool(fElement, aValue);
  222. end;
  223. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  224. function TutlXmlHelper.GetString(const aDefault: String): String;
  225. begin
  226. result := TutlXmlHelper.GetString(fElement, aDefault);
  227. end;
  228. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  229. function TutlXmlHelper.GetStringW(const aDefault: WideString): WideString;
  230. begin
  231. result := TutlXmlHelper.GetStringW(fElement, aDefault);
  232. end;
  233. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  234. function TutlXmlHelper.GetStringU(const aDefault: UnicodeString): UnicodeString;
  235. begin
  236. result := TutlXmlHelper.GetStringU(fElement, aDefault);
  237. end;
  238. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  239. function TutlXmlHelper.GetInt(const aDefault: Int64): Int64;
  240. begin
  241. result := TutlXmlHelper.GetInt(fElement, aDefault);
  242. end;
  243. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  244. function TutlXmlHelper.GetFloat(const aDefault: Double): Double;
  245. begin
  246. result := TutlXmlHelper.GetFloat(fElement, aDefault);
  247. end;
  248. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  249. function TutlXmlHelper.GetBool(const aDefault: Boolean): Boolean;
  250. begin
  251. result := TutlXmlHelper.GetBool(fElement, aDefault);
  252. end;
  253. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  254. procedure TutlXmlHelper.SetAttribString(const aName: DOMString; const aValue: String);
  255. begin
  256. fElement.SetAttributeNode(TutlXmlHelper.SetString(
  257. fElement.OwnerDocument.CreateAttribute(aName),
  258. aValue) as TDOMAttr);
  259. end;
  260. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  261. procedure TutlXmlHelper.SetAttribString(const aName: DOMString; const aValue: WideString);
  262. begin
  263. fElement.SetAttributeNode(TutlXmlHelper.SetString(
  264. fElement.OwnerDocument.CreateAttribute(aName),
  265. aValue) as TDOMAttr);
  266. end;
  267. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  268. procedure TutlXmlHelper.SetAttribString(const aName: DOMString; const aValue: UnicodeString);
  269. begin
  270. fElement.SetAttributeNode(TutlXmlHelper.SetString(
  271. fElement.OwnerDocument.CreateAttribute(aName),
  272. aValue) as TDOMAttr);
  273. end;
  274. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  275. procedure TutlXmlHelper.SetAttribInt(const aName: DOMString; const aValue: Integer);
  276. begin
  277. fElement.SetAttributeNode(TutlXmlHelper.SetInt(
  278. fElement.OwnerDocument.CreateAttribute(aName),
  279. aValue) as TDOMAttr);
  280. end;
  281. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  282. procedure TutlXmlHelper.SetAttribFloat(const aName: DOMString; const aValue: Double);
  283. begin
  284. fElement.SetAttributeNode(TutlXmlHelper.SetFloat(
  285. fElement.OwnerDocument.CreateAttribute(aName),
  286. aValue) as TDOMAttr);
  287. end;
  288. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  289. procedure TutlXmlHelper.SetAttribBool(const aName: DOMString; const aValue: Boolean);
  290. begin
  291. fElement.SetAttributeNode(TutlXmlHelper.SetBool(
  292. fElement.OwnerDocument.CreateAttribute(aName),
  293. aValue) as TDOMAttr);
  294. end;
  295. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  296. function TutlXmlHelper.GetAttribString(const aName: DOMString; const aDefault: String): String;
  297. begin
  298. result := TutlXmlHelper.GetString(fElement.Attributes.GetNamedItem(aName), aDefault);
  299. end;
  300. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  301. function TutlXmlHelper.GetAttribStringW(const aName: DOMString; const aDefault: WideString): WideString;
  302. begin
  303. result := TutlXmlHelper.GetStringW(fElement.Attributes.GetNamedItem(aName), aDefault);
  304. end;
  305. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  306. function TutlXmlHelper.GetAttribStringU(const aName: DOMString; const aDefault: UnicodeString): UnicodeString;
  307. begin
  308. result := TutlXmlHelper.GetStringU(fElement.Attributes.GetNamedItem(aName), aDefault);
  309. end;
  310. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  311. function TutlXmlHelper.GetAttribInt(const aName: DOMString; const aDefault: Int64): Int64;
  312. begin
  313. result := TutlXmlHelper.GetInt(fElement.Attributes.GetNamedItem(aName), aDefault);
  314. end;
  315. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  316. function TutlXmlHelper.GetAttribFloat(const aName: DOMString; const aDefault: Double): Double;
  317. begin
  318. result := TutlXmlHelper.GetFloat(fElement.Attributes.GetNamedItem(aName), aDefault);
  319. end;
  320. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  321. function TutlXmlHelper.GetAttribBool(const aName: DOMString; const aDefault: Boolean): Boolean;
  322. begin
  323. result := TutlXmlHelper.GetBool(fElement.Attributes.GetNamedItem(aName), aDefault);
  324. end;
  325. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  326. function TutlXmlHelper.Nodes(const aName: DOMString): IutlNodeEnumerator;
  327. begin
  328. result := TutlXmlHelper.Nodes(fElement, aName);
  329. end;
  330. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  331. function TutlXmlHelper.PrependNode(const aName: DOMString): TDOMElement;
  332. begin
  333. result := TutlXmlHelper.PrependNode(fElement, aName);
  334. end;
  335. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  336. function TutlXmlHelper.AppendNode(const aName: DOMString): TDOMElement;
  337. begin
  338. result := TutlXmlHelper.AppendNode(fElement, aName);
  339. end;
  340. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  341. procedure TutlXmlHelper.PrependText(const aText: DOMString);
  342. begin
  343. TutlXmlHelper.PrependText(fElement, aText);
  344. end;
  345. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  346. procedure TutlXmlHelper.AppendText(const aText: DOMString);
  347. begin
  348. TutlXmlHelper.AppendText(fElement, aText);
  349. end;
  350. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  351. constructor TutlXmlHelper.Create;
  352. begin
  353. inherited Create;
  354. fElement := nil;
  355. end;
  356. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  357. constructor TutlXmlHelper.Create(const aElement: TDOMElement);
  358. begin
  359. Create;
  360. fElement := aElement;
  361. end;
  362. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  363. class function TutlXmlHelper.Create(const aElement: TDOMElement): IutlXmlHelper;
  364. begin
  365. result := TutlXmlHelper.Create(aElement);
  366. end;
  367. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  368. class function TutlXmlHelper.SetString(const aNode: TDOMNode; const aValue: String): TDOMNode;
  369. begin
  370. result := aNode;
  371. if Assigned(aNode) then
  372. aNode.TextContent := DOMString(aValue);
  373. end;
  374. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  375. class function TutlXmlHelper.SetString(const aNode: TDOMNode; const aValue: WideString): TDOMNode;
  376. begin
  377. result := aNode;
  378. if Assigned(aNode) then
  379. aNode.TextContent := DOMString(aValue);
  380. end;
  381. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  382. class function TutlXmlHelper.SetString(const aNode: TDOMNode; const aValue: UnicodeString): TDOMNode;
  383. begin
  384. result := aNode;
  385. if Assigned(aNode) then
  386. aNode.TextContent := DOMString(aValue);
  387. end;
  388. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  389. class function TutlXmlHelper.SetInt(const aNode: TDOMNode; const aValue: Integer): TDOMNode;
  390. begin
  391. result := aNode;
  392. if Assigned(aNode) then
  393. aNode.TextContent := DOMString(IntToStr(aValue));
  394. end;
  395. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  396. class function TutlXmlHelper.SetFloat(const aNode: TDOMNode; const aValue: Double): TDOMNode;
  397. begin
  398. result := aNode;
  399. if Assigned(aNode) then
  400. aNode.TextContent := DOMString(FloatToStr(aValue));
  401. end;
  402. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  403. class function TutlXmlHelper.SetBool(const aNode: TDOMNode; const aValue: Boolean): TDOMNode;
  404. begin
  405. result := aNode;
  406. if Assigned(aNode) then begin
  407. if aValue
  408. then aNode.TextContent := 'true'
  409. else aNode.TextContent := 'false';
  410. end;
  411. end;
  412. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  413. class function TutlXmlHelper.GetString(const aNode: TDOMNode; const aDefault: String): String;
  414. begin
  415. if not Assigned(aNode)
  416. or ( not aNode.HasChildNodes
  417. and not (aNode is TDOMText))
  418. then result := aDefault
  419. else result := String(aNode.TextContent);
  420. end;
  421. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  422. class function TutlXmlHelper.GetStringW(const aNode: TDOMNode; const aDefault: WideString): WideString;
  423. begin
  424. if not Assigned(aNode)
  425. or ( not aNode.HasChildNodes
  426. and not (aNode is TDOMText))
  427. then result := aDefault
  428. else result := WideString(aNode.TextContent);
  429. end;
  430. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  431. class function TutlXmlHelper.GetStringU(const aNode: TDOMNode; const aDefault: UnicodeString): UnicodeString;
  432. begin
  433. if not Assigned(aNode)
  434. or ( not aNode.HasChildNodes
  435. and not (aNode is TDOMText))
  436. then result := aDefault
  437. else result := UnicodeString(aNode.TextContent);
  438. end;
  439. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  440. class function TutlXmlHelper.GetInt(const aNode: TDOMNode; const aDefault: Int64): Int64;
  441. begin
  442. if not Assigned(aNode)
  443. or ( not aNode.HasChildNodes
  444. and not (aNode is TDOMText))
  445. or not TryStrToInt64(String(aNode.TextContent), result) then
  446. result := aDefault;
  447. end;
  448. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  449. class function TutlXmlHelper.GetFloat(const aNode: TDOMNode; const aDefault: Double): Double;
  450. begin
  451. if not Assigned(aNode)
  452. or ( not aNode.HasChildNodes
  453. and not (aNode is TDOMText))
  454. or not TryStrToFloat(String(aNode.TextContent), result) then
  455. result := aDefault;
  456. end;
  457. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  458. class function TutlXmlHelper.GetBool(const aNode: TDOMNode; const aDefault: Boolean): Boolean;
  459. var
  460. s: String;
  461. begin
  462. if not Assigned(aNode)
  463. or ( not aNode.HasChildNodes
  464. and not (aNode is TDOMText))
  465. then
  466. begin
  467. result := aDefault;
  468. exit;
  469. end;
  470. s := LowerCase(String(aNode.TextContent));
  471. if (s = 'true') or (s = 't') or (s = '1') then
  472. result := true
  473. else if (s = 'false') or (s = 'f') or (s = '0') then
  474. result := false
  475. else
  476. result := aDefault;
  477. end;
  478. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  479. class function TutlXmlHelper.Nodes(const aElement: TDOMElement; const aName: DOMString): IutlNodeEnumerator;
  480. begin
  481. result := TutlNodeEnumerator.Create(aElement, aName);
  482. end;
  483. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  484. class function TutlXmlHelper.PrependNode(const aElement: TDOMElement; const aName: DOMString): TDOMElement;
  485. begin
  486. result := aElement.OwnerDocument.CreateElement(aName);
  487. if aElement.HasChildNodes
  488. then aElement.InsertBefore(result, aElement.FirstChild)
  489. else aElement.AppendChild(result);
  490. end;
  491. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  492. class function TutlXmlHelper.AppendNode(const aElement: TDOMElement; const aName: DOMString): TDOMElement;
  493. begin
  494. result := aElement.OwnerDocument.CreateElement(aName);
  495. aElement.AppendChild(result);
  496. end;
  497. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  498. class procedure TutlXmlHelper.PrependText(const aElement: TDOMElement; const aText: DOMString);
  499. var n: TDOMNode;
  500. begin
  501. n := aElement.OwnerDocument.CreateTextNode(aText);
  502. if aElement.HasChildNodes
  503. then aElement.InsertBefore(n, aElement.FirstChild)
  504. else aElement.AppendChild(n);
  505. end;
  506. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  507. class procedure TutlXmlHelper.AppendText(const aElement: TDOMElement; const aText: DOMString);
  508. begin
  509. aElement.AppendChild(aElement.OwnerDocument.CreateTextNode(aText));
  510. end;
  511. end.