[xsd-users] Re: Never failing in Parsing

ss ak ssak22 at gmail.com
Thu Aug 27 12:33:07 EDT 2009


Thanks Boris...

Last Q hope fully...when do we use this type of parsing...i mean using the
xerces calls directly..
when i m using xsd.exe to generate my classes giving xsd as input...


using namespace xercesc;

const XMLCh ls_id[] =

{xercesc::chLatin_L, xercesc::chLatin_S, xercesc::chNull};

DOMImplementation* impl (

DOMImplementationRegistry::getDOMImplementation (ls_id));

// Use the error handler implementation provided by the XSD runtime

xsd::cxx::tree::error_handler<char> eh;

xsd::cxx::xml::dom::bits::error_handler_proxy<char> ehp (eh);

xml_schema::dom::auto_ptr<DOMLSParser> parser (

impl->createLSParser (DOMImplementationLS::MODE_SYNCHRONOUS, 0));

DOMConfiguration* conf (parser->getDomConfig ());

conf->setParameter (XMLUni::fgDOMComments, false);

conf->setParameter (XMLUni::fgDOMDatatypeNormalization, true);

conf->setParameter (XMLUni::fgDOMEntities, false);

conf->setParameter (XMLUni::fgDOMNamespaces, true);

conf->setParameter (XMLUni::fgDOMElementContentWhitespace, false);

// Set error handler.

//

conf->setParameter (XMLUni::fgDOMErrorHandler, &ehp);

if (validate)

{

conf->setParameter (XMLUni::fgDOMValidate, true);

conf->setParameter (XMLUni::fgXercesSchema, true);

conf->setParameter (XMLUni::fgXercesSchemaFullChecking, false);

// If we are validating, pre-load and cache the schema.

//

parser->loadGrammar ("test.xsd", Grammar::SchemaGrammarType, true);

conf->setParameter (XMLUni::fgXercesUseCachedGrammarInParse, true);

}

else

{

conf->setParameter (XMLUni::fgDOMValidate, false);

conf->setParameter (XMLUni::fgXercesSchema, false);

conf->setParameter (XMLUni::fgXercesSchemaFullChecking, false);

}

conf->setParameter (XMLUni::fgXercesUserAdoptsDOMDocument, true);

// Create memory buffer input source.

MemBufInputSource is (reinterpret_cast<XMLByte*> (buf), size, file, false);

is.setCopyBufToStream (false);

Wrapper4InputSource wis (&is, false);

// Parsing loop.

os::time start;

for (unsigned long i (0); i < iter; ++i)

{// First parse XML to DOM reusing the parser we created above.

xml_schema::dom::auto_ptr<DOMDocument> doc (parser->parse (&wis));

eh.throw_if_failed<xml_schema::parsing> ();

// Then parse DOM to the object model.

auto_ptr<test::root> r (test::root_ (*doc));

}

Please ...let me know when we use this type of thing...when using xsd to
generate all my classes ...




On Thu, Aug 27, 2009 at 6:32 AM, Boris Kolpackov <boris at codesynthesis.com>wrote:

> Hi,
>
> I see you have figured out how to serialize the object model back
> to XML. In the future, please don't send multiple emails asking
> essentially the same question. One is enough and someone will reply
> when/if they have time. If you need more predictable assistance, I
> suggest you consider purchasing a Priority Support contract:
>
> http://www.codesynthesis.com/support/
>
>
> ss ak <ssak22 at gmail.com> writes:
>
> > but what ever values given,parser never gives an exception...
>
> You mean serializer, not parser? Your code below doesn't perform
> any parsing, only serialization.
>
>
> > xml_schema::namespace_infomap map;
> >
> > map[""].schema = "test.xsd";
> >
> > std::ostringstream oss;
> >
> > nt_(oss, var_nt, map);  //here nt is my class generated
> >
> > std::string xml (oss.str ());
> >
> > printf("\n XML %s",xml.c_str()); //here it prints the xml valid or not
>
> The C++/Tree mapping doesn't support automatic XML Schema validation
> during serialization. What you can do, however, is re-parse the XML
> to make sure it is valid. So given the code above, you can add:
>
> try
> {
>  std::istringstream iss (xml);
>  nt_(iss, "<in-memory-xml>");
> }
> catch (const xml_schema::exception& e)
> {
>  std::cerr << e << std::endl;
> }
>
> Boris
>



More information about the xsd-users mailing list