i know you said you didn't want a separate XML library; but maybe someone else would like the sample code:
var
sax: SAXXMLReader60;
stm: IStream;
begin
//Get a stream around our large file
stm := TStreamAdapter.Create(TFileStream.Create('USGovBudgetLineItems2008.xml', fmOpenRead ));
sax := CoSAXXMLReader60.Create;
sax.contentHandler := TVBSAXContentHandler.Create;
sax.parse(stm);
end;
And we listen for the events with our SAXContentHandler
object.
For all the IDispatch
events you can return E_NOTIMPL
(msxml doesn't even call them).
All the rest you can plug in whatever code you want:
TVBSAXContentHandler = class(TInterfacedObject, IVBSAXContentHandler)
protected
{ IDispatch }
function GetTypeInfoCount(out Count: Integer): HResult; stdcall;
function GetTypeInfo(Index, LocaleID: Integer; out TypeInfo): HResult; stdcall;
function GetIDsOfNames(const IID: TGUID; Names: Pointer; NameCount, LocaleID: Integer; DispIDs: Pointer): HResult; stdcall;
function Invoke(DispID: Integer; const IID: TGUID; LocaleID: Integer; Flags: Word; var Params; VarResult, ExcepInfo, ArgErr: Pointer): HResult; stdcall;
public
{ IVBSAXContentHandler }
procedure Set_documentLocator(const Param1: IVBSAXLocator); safecall;
procedure startDocument; safecall;
procedure endDocument; safecall;
procedure startPrefixMapping(var strPrefix: WideString; var strURI: WideString); safecall;
procedure endPrefixMapping(var strPrefix: WideString); safecall;
procedure startElement(var strNamespaceURI: WideString; var strLocalName: WideString;
var strQName: WideString; const oAttributes: IVBSAXAttributes); safecall;
procedure endElement(var strNamespaceURI: WideString; var strLocalName: WideString;
var strQName: WideString); safecall;
procedure characters(var strChars: WideString); safecall;
procedure ignorableWhitespace(var strChars: WideString); safecall;
procedure processingInstruction(var strTarget: WideString; var strData: WideString); safecall;
procedure skippedEntity(var strName: WideString); safecall;
// property documentLocator: IVBSAXLocator write Set_documentLocator;
end;
Note: Any code is released into the public domain. No attribution required.
XPath
to locate a parse a group of nodes, instead of iterate over all the nodes at once. – RRUZ