Thema: Delphi MXXMLWriter und Delphi

Einzelnen Beitrag anzeigen

mc_adams

Registriert seit: 18. Apr 2008
7 Beiträge
 
#3

Re: MXXMLWriter und Delphi

  Alt 11. Jul 2008, 10:13
Danke erstmal für die schelle Hilfe.
Aber bei dem von dir verlinkten Code gabe es bei mir auch eine Zugriffsverletzung (Out of Range). Aus diesem Grund habe ich mal eine andere Version der MSXML ausprobiert. Und siehe da mit der Version 6 (vorher Version 4) funktioniert der oben beschrieben Code. Ich habe daraus mal eine Klasse gemacht.

Hier der Code

[delphi]

unit XMLUtils;

interface

uses
Classes, MSXML2_TLB;

type
TXMLWriter60 = class(TObject)
private
FLevel: Integer;
FAttributes: IMXAttributes;
FContentHandler: IVBSAXContentHandler;
FWriter: IMXWriter;
function GetEncoding: Widestring;
procedure SetEncoding(const Value: Widestring);
function GetOutput: Variant;
procedure SetOutput(const Value: Variant);
public
constructor Create;
procedure AddAttributes(const QName: WideString; const Value: WideString);
overload;
procedure AddAttributes(const URI: WideString; const LocalName: WideString;
const QName: WideString; strType: WideString; const Value: WideString);
overload;
procedure ClearAttributes;
procedure EndDocument;
procedure EndElement(Name: Widestring); overload;
procedure EndElement(NamespaceURI, LocalName, Name: Widestring); overload;
procedure StartDocument;
procedure StartElement(Name: Widestring); overload;
procedure StartElement(NamespaceURI, LocalName, Name: Widestring); overload;
procedure IgnorableWhitespace(strChars: WideString);

property Attributes: IMXAttributes read FAttributes;
property Encoding: Widestring read GetEncoding write SetEncoding;
property Output: Variant read GetOutput write SetOutput;
end;

implementation

//------------------------------------------------------------------------------
function TXMLWriter60.GetEncoding: Widestring;
begin
Result := FWriter.encoding;
end;

//------------------------------------------------------------------------------
procedure TXMLWriter60.SetEncoding(const Value: Widestring);
begin
FWriter.encoding := Value;
end;

//------------------------------------------------------------------------------
function TXMLWriter60.GetOutput: Variant;
begin
Result := FWriter.output;
end;

procedure TXMLWriter60.IgnorableWhitespace(strChars: WideString);
begin
FContentHandler.ignorableWhitespace(strChars);
end;

//------------------------------------------------------------------------------
procedure TXMLWriter60.SetOutput(const Value: Variant);
begin
FWriter.output := Value;
end;

//------------------------------------------------------------------------------
procedure TXMLWriter60.AddAttributes(const QName: WideString; const Value:
WideString);
begin
AddAttributes('', '', QName, '', Value);
end;

//------------------------------------------------------------------------------
procedure TXMLWriter60.AddAttributes(const URI: WideString; const LocalName:
WideString; const QName: WideString; strType: WideString; const Value:
WideString);
begin
FAttributes.addAttribute(URI, LocalName, QName, strType, Value);
end;

//------------------------------------------------------------------------------
procedure TXMLWriter60.ClearAttributes;
begin
FAttributes.clear;
end;

//------------------------------------------------------------------------------
constructor TXMLWriter60.Create;
begin
inherited;
FWriter := CoMXXMLWriter60.Create;
FAttributes := CoSAXAttributes60.Create;
FContentHandler := FWriter as IVBSAXContentHandler;
end;

//------------------------------------------------------------------------------
procedure TXMLWriter60.EndDocument;
begin
FContentHandler.endDocument;
end;

//------------------------------------------------------------------------------
procedure TXMLWriter60.EndElement(Name: Widestring);
begin
EndElement('', '', Name);
end;

//------------------------------------------------------------------------------
procedure TXMLWriter60.EndElement(NamespaceURI, LocalName, Name: Widestring);
begin
FContentHandler.endElement(NamespaceURI, LocalName, Name);
if FLevel <> 0 then
IgnorableWhitespace(#13);
Dec(FLevel);
end;

//------------------------------------------------------------------------------
procedure TXMLWriter60.StartDocument;
begin
FContentHandler.startDocument;
FLevel := 0;
end;

//------------------------------------------------------------------------------
procedure TXMLWriter60.StartElement(Name: Widestring);
begin
StartElement('', '', Name);
end;

//------------------------------------------------------------------------------
procedure TXMLWriter60.StartElement(NamespaceURI, LocalName, Name: Widestring);
begin
if FLevel <> 0 then
IgnorableWhitespace(#13);

FContentHandler.startElement(NamespaceURI, LocalName, Name, FAttributes as
IVBSAXAttributes);

Inc(FLevel);
end;

end.


Also bis zum nächsten mal
  Mit Zitat antworten Zitat