GLX.CrossXML.pas 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //
  2. // The unit for GLXcene Engine
  3. //
  4. {
  5. }
  6. unit GLX.CrossXML;
  7. interface
  8. uses
  9. System.Classes,
  10. System.SysUtils,
  11. Variants,
  12. XMLIntf,
  13. XMLDoc,
  14. XMLDom;
  15. type
  16. GLSXMLDocument = IXMLDocument;
  17. GLSXMLNode = IXMLNode;
  18. GLSDOMNode = IDOMNode;
  19. function GLSNewXMLDocument: GLSXMLDocument;
  20. procedure ReleaseXMLDocument(var ADoc: GLSXMLDocument);
  21. procedure WriteXMLFile(var ADoc: GLSXMLDocument; AStream: TStream); overload;
  22. procedure ReadXMLFile(var ADoc: GLSXMLDocument; AStream: TStream); overload;
  23. procedure WriteXMLFile(var ADoc: GLSXMLDocument; AFileName: string); overload;
  24. procedure ReadXMLFile(var ADoc: GLSXMLDocument; AFileName: string); overload;
  25. function GetXMLAttribute(const XMLNode: GLSXMLNode; const AttrName: string; out Value: string): Boolean; overload;
  26. function GetXMLAttribute(const XMLNode: GLSXMLNode; Idx: Integer): GLSXMLNode; overload;
  27. procedure SetXMLAttribute(const XMLNode: GLSXMLNode; const AttrName: string; const Value: string); overload;
  28. procedure SetXMLAttribute(const DOMNode: GLSDOMNode; const AttrName: string; const Value: string); overload;
  29. function GetXMLAttributeCount(const XMLNode: GLSXMLNode): Integer;
  30. function FindXMLNode(const ParentNode: GLSXMLNode; const NodeName: string; out ChildNode: GLSXMLNode): Boolean;
  31. function CreateDOMNode(const ParentNode: GLSDOMNode; const NodeName: string): GLSDOMNode;
  32. procedure SetXMLText(const DOMNode: GLSDOMNode; const AText: string);
  33. function GetXMLText(const XMLNode: GLSXMLNode; out AText: string): Boolean;
  34. implementation
  35. function GLSNewXMLDocument: GLSXMLDocument;
  36. begin
  37. Result := NewXMLDocument();
  38. end;
  39. procedure ReleaseXMLDocument(var ADoc: GLSXMLDocument);
  40. begin
  41. ADoc := nil;
  42. end;
  43. procedure WriteXMLFile(var ADoc: GLSXMLDocument; AStream: TStream);
  44. begin
  45. ADoc.SaveToStream(AStream);
  46. end;
  47. procedure ReadXMLFile(var ADoc: GLSXMLDocument; AStream: TStream);
  48. begin
  49. ADoc.LoadFromStream(AStream);
  50. end;
  51. procedure WriteXMLFile(var ADoc: GLSXMLDocument; AFileName: string); overload;
  52. begin
  53. ADoc.SaveToFile(AFileName);
  54. end;
  55. procedure ReadXMLFile(var ADoc: GLSXMLDocument; AFileName: string); overload;
  56. begin
  57. ADoc.LoadFromFile(AFileName);
  58. end;
  59. function GetXMLAttribute(const XMLNode: GLSXMLNode; const AttrName: string; out Value: string): Boolean;
  60. var
  61. attr: OleVariant;
  62. begin
  63. attr := 0;
  64. attr := XMLNode.Attributes[AttrName];
  65. Result := not VarIsNull(attr);
  66. if Result then
  67. Value := attr;
  68. end;
  69. procedure SetXMLAttribute(const XMLNode: GLSXMLNode; const AttrName: string; const Value: string);
  70. begin
  71. XMLNode.Attributes[AttrName] := Value;
  72. end;
  73. procedure SetXMLAttribute(const DOMNode: GLSDOMNode; const AttrName: string; const Value: string);
  74. var
  75. E: IDOMElement;
  76. begin
  77. E := DOMNode as IDOMElement;
  78. E.SetAttribute(AttrName, Value);
  79. end;
  80. function FindXMLNode(const ParentNode: GLSXMLNode; const NodeName: string; out ChildNode: GLSXMLNode): Boolean;
  81. begin
  82. ChildNode := ParentNode.ChildNodes.FindNode(NodeName);
  83. Result := Assigned(ChildNode);
  84. end;
  85. function CreateDOMNode(const ParentNode: GLSDOMNode; const NodeName: string): GLSDOMNode;
  86. begin
  87. Result := ParentNode.OwnerDocument.CreateElement(NodeName);
  88. ParentNode.AppendChild(Result);
  89. end;
  90. procedure SetXMLText(const DOMNode: GLSDOMNode; const AText: string);
  91. begin
  92. DOMNode.AppendChild(DOMNode.ownerDocument.createTextNode(AText));
  93. end;
  94. function GetXMLText(const XMLNode: GLSXMLNode; out AText: string): Boolean;
  95. begin
  96. AText := XMLNode.Text;
  97. Result := Length(AText)>0;
  98. end;
  99. function GetXMLAttributeCount(const XMLNode: GLSXMLNode): Integer;
  100. begin
  101. Result := XMLNode.AttributeNodes.Count;
  102. end;
  103. function GetXMLAttribute(const XMLNode: GLSXMLNode; Idx: Integer): GLSXMLNode;
  104. begin
  105. Result := XMLNode.AttributeNodes[Idx];
  106. end;
  107. end.