![]() |
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. |
Re: MXXMLWriter und Delphi
|
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 12:02 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz