Delphi Wrapper Classes and XML
(Page 1 of 4 )
In this article we will look at a partial listing of the xmlintf unit and then write a program to demonstrate how to use DOM in Delphi. We began our discussion of this topic in the previous article. This is part three of a four-part series.
Below is a listing of the xmlintf unit that we discussed in the previous article. It is one of the main XML units that form part of the wrapper classes in Delphi. I will only show the functions, procedures and properties definitions and not their implementation, because it would fill the entire article if I did:
unit XMLIntf;
interface
{$IFDEF MSWINDOWS}
{ MSXML.H defines another IXMLDocument and references the type...
The compiler must see this before it sees the IXMLDocument
introduced in this unit - otherwise, references will cause
ambiguity errors }
(*$HPPEMIT '#include <msxml.h>' *)
{$ENDIF}
uses SysUtils, Classes, xmldom;
type
{ Data types }
TNodeType = (ntReserved, ntElement, ntAttribute, ntText, ntCData,
ntEntityRef, ntEntity, ntProcessingInstr, ntComment, ntDocument,
ntDocType, ntDocFragment, ntNotation);
{ Forward Declarations }
IXMLNode = interface;
IXMLNodeList = interface;
IXMLNodeCollection = interface;
IXMLDocument = interface;
{ EXMLDocError }
EXMLDocError = class(Exception)
end;
{ IXMLNode }
IXMLNode = interface
['{395950C0-7E5D-11D4-83DA-00C04F60B2DD}']
{ Property Accessors }
function GetAttribute(const AttrName: DOMString): OleVariant;
function GetAttributeNodes: IXMLNodeList;
function GetChildNodes: IXMLNodeList;
function GetChildValue(const IndexOrName: OleVariant): OleVariant;
function GetCollection: IXMLNodeCollection;
function GetDOMNode: IDOMNode;
function GetHasChildNodes: Boolean;
function GetIsTextElement: Boolean;
function GetLocalName: DOMString;
function GetNamespaceURI: DOMString;
function GetNodeName: DOMString;
function GetNodeType: TNodeType;
function GetNodeValue: OleVariant;
function GetOwnerDocument: IXMLDocument;
function GetParentNode: IXMLNode;
function GetPrefix: DOMString;
function GetReadOnly: Boolean;
function GetText: DOMString;
function GetXML: DOMString;
procedure SetAttribute(const AttrName: DOMString; const Value: OleVariant);
procedure SetChildValue(const IndexOrName: OleVariant; const Value: OleVariant);
procedure SetNodeValue(const Value: OleVariant);
procedure SetReadOnly(const Value: Boolean);
procedure SetText(const Value: DOMString);
{ Methods }
function AddChild(const TagName: DOMString; Index: Integer = -1): IXMLNode; overload;
function AddChild(const TagName, NamespaceURI: DOMString;
GenPrefix: Boolean = False; Index: Integer = -1): IXMLNode; overload;
function CloneNode(Deep: Boolean): IXMLNode;
procedure DeclareNamespace(const Prefix, URI: DOMString);
function FindNamespaceURI(const TagOrPrefix: DOMString): DOMString;
function FindNamespaceDecl(const NamespaceURI: DOMString): IXMLNode;
function GetAttributeNS(const AttrName, NamespaceURI: DOMString): OleVariant;
function HasAttribute(const Name: DOMString): Boolean; overload;
function HasAttribute(const Name, NamespaceURI: DOMString): Boolean; overload;
function NextSibling: IXMLNode;
procedure Normalize;
function PreviousSibling: IXMLNode;
procedure Resync;
procedure SetAttributeNS(const AttrName, NamespaceURI: DOMString; const Value: OleVariant);
procedure TransformNode(const stylesheet: IXMLNode; var output: WideString); overload;
procedure TransformNode(const stylesheet: IXMLNode; const output: IXMLDocument); overload;
The functions and procedures above define some of the elements that make up the wrapper class. Below are the properties and attributes of the class:
{ Properties }
property Attributes[const AttrName: DOMString]: OleVariant read GetAttribute write SetAttribute;
property AttributeNodes: IXMLNodeList read GetAttributeNodes;
property ChildNodes: IXMLNodeList read GetChildNodes;
property ChildValues[const IndexOrName: OleVariant]: OleVariant read GetChildValue write SetChildValue; default;
property Collection: IXMLNodeCollection read GetCollection;
property DOMNode: IDOMNode read GetDOMNode;
property OwnerDocument: IXMLDocument read GetOwnerDocument;
property HasChildNodes: Boolean read GetHasChildNodes;
property IsTextElement: Boolean read GetIsTextElement;
property LocalName: DOMString read GetLocalName;
property NamespaceURI: DOMString read GetNameSpaceURI;
property NodeName: DOMstring read GetNodeName;
property NodeType: TNodeType read GetNodeType;
property NodeValue: OleVariant read GetNodeValue write SetNodeValue;
property ParentNode: IXMLNode read GetParentNode;
property Prefix: DOMString read GetPrefix;
property ReadOnly: Boolean read GetReadOnly write SetReadOnly;
property Text: DOMString read GetText write SetText;
property XML: DOMString read GetXML;
end;
{ IXMLNodeList }
Below I've defined the interface to the XML wrapper class. It will be used by the other classes and units to interface. The methods and properties are also defined:
IXMLNodeList = interface
['{395950C1-7E5D-11D4-83DA-00C04F60B2DD}']
{ Property Accessors }
function GetCount: Integer;
function GetNode(const IndexOrName: OleVariant): IXMLNode;
function GetUpdateCount: Integer;
{ Methods }
function Add(const Node: IXMLNode): Integer;
procedure BeginUpdate;
procedure Clear;
function Delete(const Index: Integer): Integer; overload;
function Delete(const Name: DOMString): Integer; overload;
function Delete(const Name, NamespaceURI: DOMString): Integer; overload;
procedure EndUpdate;
function First: IXMLNode;
function FindNode(NodeName: DOMString): IXMLNode; overload;
function FindNode(NodeName, NamespaceURI: DOMString): IXMLNode; overload;
function FindNode(ChildNodeType: TGuid): IXMLNode; overload;
function FindSibling(const Node: IXMLNode; Delta: Integer): IXMLNode;
function Get(Index: Integer): IXMLNode;
function IndexOf(const Node: IXMLNode): Integer; overload;
function IndexOf(const Name: DOMString): Integer; overload;
function IndexOf(const Name, NamespaceURI: DOMString): Integer; overload;
procedure Insert(Index: Integer; const Node: IXMLNode);
function Last: IXMLNode;
function Remove(const Node: IXMLNode): Integer;
function ReplaceNode(const OldNode, NewNode: IXMLNode): IXMLNode;
{ Properties }
property Count: Integer read GetCount;
property Nodes[const IndexOrName: OleVariant]: IXMLNode read GetNode; default;
property UpdateCount: Integer read GetUpdateCount;
end;
The XMLnode collection interface is then created:
{ IXMLNodeCollection }
IXMLNodeCollection = interface(IXMLNode)
['{395950C2-7E5D-11D4-83DA-00C04F60B2DD}']
{ Property Accessors }
function GetCount: Integer;
function GetNode(Index: Integer): IXMLNode;
{ Public properties }
procedure Clear;
procedure Delete(Index: Integer);
function Remove(const Node: IXMLNode): Integer;
property Count: Integer read GetCount;
property Nodes[Index: Integer]: IXMLNode read GetNode; default;
end;
Next: XML Properties, Methods, and Interface >>
More Delphi-Kylix Articles
More By David Web