tags:

views:

25

answers:

1

I am using SAX2 in Xerces C++ and would like to get XML Schema data while I handle elements so that I know their type defined in the Schema. How can I accomplish this?

A: 

Okay, I figured out how to do this. Sparse documentation available on the subject. Apparently I need to cast the SAX2XMLReader that XMLReaderFactory::createXMLReader() returns, to a SAX2XMLReaderImpl. Then I can register an PSVIHandler implementation on that interface. I have to provide my own implementation of the PSVIHandler, as I could not find a default implementation within Xerces.

Once this implementation of PSVI is registered with the SAX2XMLReaderImpl, I then create an ContentHandler impl and pass the PSVI handler impl to its constructor. Then I register the ContentHandler with SAX2XMLReaderImpl. Then when I am parsing I can access information from PSVIHandler to get schema related info.

It all seems very clumsy and the PSVIHandler interface seems very unfriendly. Maybe there is a better way.

Here is a code snippet:

  SAX2XMLReaderImpl* parser = dynamic_cast<SAX2XMLReaderImpl*>(XMLReaderFactory::createXMLReader());
  PSVIHandler* pSchemaHandler = new MyPSVIHandler();
  DefaultHandler* defaultHandler = new MyXMLHandler(pSchemaHandler);
  parser->setContentHandler(defaultHandler);
  parser->setErrorHandler(defaultHandler);
  parser->setPSVIHandler(pSchemaHandler);
anio