Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2005-12

sax.pp

6 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Tony Pelton

hi all,

i'm a pascal noob, but not a new programmer.

i've done a fair bit of XML parser work in Java.

i was trying to figure out how to do parsing with SAX in pascal last night.

i'm very confused.

in java there are basically three steps to doing SAX parsing :

1) subclass a "handler" and override the methods for the parsing events you want to trap, such as "begin element".

2) create an instance of the XML parser, and pass an instance of your "handler" to it.

3) pass in an XML document to the parser and tell it to parse.

your handler then gets callbacks from the parser with data from the xml document.

in in the 'fcl' for free pascal ... i got confused.

for one thing, i don't see any code in the source file that looks like it is actually doing any parsing ?

i expected to see a bunch of code in there somewhere that was doing a bunch of string comparison and manipulations at the character level, like and parser would have to.

maybe there is other code that i haven't looked at that fills in some of the gaps ?

second, for hoots and hollers, i subclassed/extended the SAXFilter class ... thinking that it looked like the descendent of everything else, thinking that it was itself the parser and handler, and tried to override some of the callback functions ... but when i call parse on it, i get an error about abstract methods.

i'm lost.

help ?

documentation ? sample code ?

Tony

L505
to Tony Pelton
Show quoted text 5 lines
second, for hoots and hollers, i subclassed/extended the SAXFilter class ... thinking that it looked like the descendent of everything else, thinking that it was itself the parser and handler, and tried to override some of the callback functions ... but when i call parse on it, i get an error about abstract methods.

If you forget the override keyword after a function declaration, you may get abstract errors. You can't call an abstract function directly. I'm not sure if this has anything to do with it, but maybe Java's class ancestor/inheritance system is a bit different causing you issues since you come from that background and have memorized how to code classes that way.

Would need to see some sample code that is giving you the abstract error, though. I just finished dealing with an "abstract error" and all I did was forget to place the override keyword after the function declaration in the *derivative* class.

Just to add to the confusion: Tstrings is abstract.. so calling something like TStrings.create isn't right but TStringList.create is okay.

begin
  ObjectOrientation:= confusion
end;
Tony Pelton
to L505
L505 wrote
Would need to see some sample code that is giving you the abstract error, though. I just finished dealing with an "abstract error" and all I did was forget to place the override keyword after the function declaration in the *derivative* class.

thanks for the response.

i'm including some code.

it compiles, but generates a runtime error :

An unhandled exception occurred at $0040691E :
EAbstractError : Abstract method called

with java, unless you are hiding the implementation from the compiler, by using runtime classloading for instance, the compiler will tell you that you are trying to use an abstract class and what abstract thing you are trying to use and that it is bad and wrong.

so this pascal code wouldn't get past the compiler were it java.

note that i was just trying to get to a starting point of being able to capture "character" events from the parser and write them to stdout, nothing more.

also note that i've got all of the other events commented out for starters.

i must be missing something blatant, likely due to my inexperience with pascal.

thanks for any help.

Tony

PS. btw, a url (http://x-plane.dsrts.com/screenshot_0.bmp) with a screenshot of a "plugin" i've written for a flight simulator using FPC, and to which this issue is related.

all of the "brightly colored" bits that you see in the screenshot are the work of my plugin within the simulator and is all written in pascal as a windows DLL using OpenGL calls.

The simulator API also supports 'C', but I had come close to suicide on more than one occassion trying to write 'C' and had almost given up, when i thought to try FreePascal.

i've never looked back.

'C' sucks, FreePascal ROCKS !

-- SNIP --
program Main;

Uses
// heaptrc,
SAX,
Classes,
Objects,
Math,
StrUtils,
SysUtils;

Type Tmyfilter = class(TSAXFilter)
procedure DoCharacters(const ch: PSAXChar; AStart, ALength: Integer); override;
end;
procedure Tmyfilter.DoCharacters(const ch: PSAXChar; AStart, ALength: Integer);
begin
WriteLn(StrPas(PChar(ch)));
end;
{
procedure DoCharacters(const ch: PSAXChar; AStart, ALength: Integer); dynamic;
procedure DoComment(const ch: PSAXChar; AStart, ALength: Integer); dynamic;
procedure DoEndDocument; dynamic;
procedure DoEndElement(const NamespaceURI, LocalName, QName:
SAXString); dynamic;
procedure DoEndPrefixMapping(const Prefix: SAXString); dynamic;
procedure DoIgnorableWhitespace(const ch: PSAXChar; AStart, ALength:
Integer); dynamic;
procedure DoProcessingInstruction(const Target, Data: SAXString); dynamic;
procedure DoSkippedEntity(const Name: SAXString); dynamic;
procedure DoStartDocument; dynamic;
procedure DoStartElement(const NamespaceURI, LocalName, QName:
SAXString; Atts: TSAXAttributes); dynamic;
procedure DoStartPrefixMapping(const Prefix, URI: SAXString); dynamic;
}
Type TDosStreamPtr = ^TDosStream;
Var a_name : FNameStr;
Var a_stream : TDosStreamPtr;
Var a_filter : Tmyfilter;
begin
a_name := 'google_earth_tutorial_files.kml';
a_stream := new(TDosStreamPtr,Init(a_name,stOpenRead));
a_filter := TmyFilter.Create;
a_filter.ParseStream(@a_stream);
end.
Sebastian Günther
to Tony Pelton: i'm a pascal noob, but not a new programmer. i've done a fair bit of XML parser work in…
Show quoted text 9 lines
hi all, i'm a pascal noob, but not a new programmer. i've done a fair bit of XML parser work in Java. i was trying to figure out how to do parsing with SAX in pascal last night. i'm very confused.

Okay I can imagine this... ;)

the actual SAX support is for HTML parsing only. The XML parser can only create DOM documents, without using the SAX layer.

Regards,
Sebastian

Tony Pelton
to Sebastian Günther
Sebastian Günther wrote 15 lines
Tony Pelton schrieb: > hi all, > > i'm a pascal noob, but not a new programmer. > > i've done a fair bit of XML parser work in Java. > > i was trying to figure out how to do parsing with SAX in pascal last night. > > i'm very confused. Okay I can imagine this... ;) the actual SAX support is for HTML parsing only. The XML parser can only create DOM documents, without using the SAX layer.

hmmm ...

ok, let me make sure i understand.

the code in "sax.pp" is not for general use as a sax parser but is in support of some other code, somewhere else, for doing html parsing ?

ok.

that's kind of a bummer.

i guess i'll have to go back to the drawing board for what i'm trying to do.

marshalling the entire XML document into memory is potentially problematic for my application.

Regards, Sebastian

thanks,
Tony

Sebastian Günther
to Tony Pelton
Show quoted text 11 lines
> >the actual SAX support is for HTML parsing only. The XML parser can only >create DOM documents, without using the SAX layer. hmmm ... ok, let me make sure i understand. the code in "sax.pp" is not for general use as a sax parser but is in support of some other code, somewhere else, for doing html parsing ?

The SAX unit is a high level implementation of the SAX specifiation. The SAX_HTML unit contains a HTML to SAX converter class, which will translate HTML parsing events to SAX events.

The original XML parser (which is really old by now; I wrote it on a lonely Sunday years ago) directly constructs DOM documents.

I've started a SAX based XML parser some time ago, but the initial approach showed to be much slower than the old parser. As long as the old XML parser has been used for non-Unicode documents and as long as XML namespaces didn't care, this parse did good work for me and lots of other people as well.
Actually I'm not really sure which way is the best to continue. Even SAX has his advantages and disadvantages.

- Sebastian