Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   XML (https://www.delphipraxis.net/46-xml/)
-   -   Delphi MXXMLWriter und Delphi (https://www.delphipraxis.net/117042-mxxmlwriter-und-delphi.html)

mc_adams 10. Jul 2008 14:15


MXXMLWriter und Delphi
 
Hallo Leidensgenossen

Ich möchte ein Funktion schreiben, die mir eine XML Struktur erzeugt. Nach längerem Studium der MSDN habe ich mich für den MXXMLWriter (MSXML 4.0) entschieden. Leider bricht meine Funktion schon beim Anlegen des erstens Knoten (startElement) mit der Exception "Falscher Parameter" ab.

Ich hoffe mir kann jemand helfen.

Hier mein Code:

function CreateTestXmlAsText: Widestring;
var
Name: Widestring;
NoValue: Widestring;
Writer: IMXWriter;
Attributes: IMXAttributes;
ContentHandler: IVBSAXContentHandler;
begin
Writer := CoMXXMLWriter40.Create;
Writer.byteOrderMark := True;
Writer.omitXMLDeclaration := False;
Writer.indent := True;
Writer.encoding := 'UTF-8';

ContentHandler := Writer as IVBSAXContentHandler;
Attributes := CoSAXAttributes.Create;

ContentHandler.startDocument;

Name := 'TestNode';
ContentHandler.startElement(NoValue, NoValue, Name, Attributes as IVBSAXAttributes);
ContentHandler.endElement(NoValue, NoValue, Name);

ContentHandler.endDocument;

Result := Writer.output;
end;

Schon mal Danke im voraus.

marabu 10. Jul 2008 14:47

Re: MXXMLWriter und Delphi
 
Hallo,

vielleicht kannst du hier etwas Nektar saugen: klick

Grüße vom marabu

mc_adams 11. Jul 2008 09:13

Re: MXXMLWriter und Delphi
 
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


Alle Zeitangaben in WEZ +1. Es ist jetzt 03:05 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz