[xsd-users] Parser maintains state between parsing attempts?

Boris Kolpackov boris at codesynthesis.com
Tue Jul 11 10:22:35 EDT 2006


Hi Gerard,

Gerard Lanois <gerardlanois at gmail.com> writes:

> Version 2 of my program - doesn't work (explanation below).
>
> char* msg = ... // read text from somewhere
> std::istringstream iss(msg);
>
> try {
>  std::auto_ptr<Apple::type> a(Apple(iss, xml_schema::flags::dont_validate));
> }
> catch (...) {
> }
>
> try {
>  std::auto_ptr<Bannana::type> b(Bannana(iss,
> xml_schema::flags::dont_validate));
> }
> catch (...) {
> }
>
>
> In this version of the program, the attempted parsing of an incoming
> Bannana causes the Apple to throw an execption, as you would
> expect.
>
> However, when the program makes the attempt to parse the
> Bannana as a Bannana, the parser fails.

This is because the call to the Apple parser function has read all
the data from iss (i.e., iss internal position is at the end of
the string). You can fix this by "rewinding" the stream in your
second try block:

try
{
  iss.seekg (0); // rewind to the beginning
  iss.clear ();  // clear eof bit

  std::auto_ptr<Bannana::type> b(Bannana(iss,
    xml_schema::flags::dont_validate));
}


> What I'm trying to do:  I have an input stream which can have two
> types of documents arriving (Apple, and Bannana).  I don't have
> any way of knowing in advance what is going to arrive.  Is there
> an easier way to find out what the document type is before
> submitting it?  (brute force peeking into the document?)

The approach above should work though it is definitely not the
most efficient one especially if your messages are large. If you
want to get the best performance then you can split the parsing
into two steps: parsing of XML to DOM and parsing DOM to Apple or
Banana. Between the two stages you can insert a check which looks
at the name of the root element and decides which parsing function
to call.

hth,
-boris
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 652 bytes
Desc: Digital signature
Url : http://codesynthesis.com/pipermail/xsd-users/attachments/20060711/db6a4fcf/attachment.pgp


More information about the xsd-users mailing list