You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-users@xerces.apache.org by Sebastian 'CrashandDie' Lauwers <cr...@gmail.com> on 2008/05/27 20:28:53 UTC

Validating XML with XSD and DOM Parser

Hi everyone,

I'm trying to validate get the DOM parser to validate an XML instance
through an XSD file, though, I'm not really sure as to how to do
this...

I've Googled quite a bit, but I think I'm looking in the wrong direction.

XSD file: http://slexy.org/view/s2iGvEBQAU
Relevant code: http://slexy.org/view/s2c2p1D8HG

Could you point me in the right direction ? Any help is appreciated, really.

Best regards,

S.

-- 
question = ( to ) ? be : ! be;
 -- Wm. Shakespeare

Re: Validating XML with XSD and DOM Parser

Posted by Roberto Rigamonti <ro...@gmail.com>.
You're right, I've simply copied it from old buggy code (I'm rewriting
my app from scratch, so it's temporary shattered and I had to refer to
the test one).
My pleasure!
                 Roberto

Sebastian 'CrashandDie' Lauwers wrote:
> On Tue, May 27, 2008 at 8:52 PM, Roberto Rigamonti <ro...@gmail.com> wrote:
>> Hi, I had the same problem and I solved it in this way:
>> <snip code>
> 
> Awesome !
> 
> Thanks for your help.
> 
> This is what I ended up with, the code you provided had a few memory
> leaks, but it helped out a whole bunch ! Thank you so much !


Re: Validating XML with XSD and DOM Parser

Posted by Sebastian 'CrashandDie' Lauwers <cr...@gmail.com>.
On Tue, May 27, 2008 at 8:52 PM, Roberto Rigamonti <ro...@gmail.com> wrote:
> Hi, I had the same problem and I solved it in this way:
> <snip code>

Awesome !

Thanks for your help.

This is what I ended up with, the code you provided had a few memory
leaks, but it helped out a whole bunch ! Thank you so much !

xmldomerrorreporter.hpp:

-- code starts --

#ifndef __XMLDOMERRORREPORTER__
#define __XMLDOMERRORREPORTER__

#include <xercesc/sax/ErrorHandler.hpp>
#include <xercesc/sax/SAXParseException.hpp>
#include <iostream>

class XMLDOMErrorReporter : public xercesc::ErrorHandler
{

	private:
	bool sawErrors;

	public:
	inline XMLDOMErrorReporter () : sawErrors (false) { }
	inline ~XMLDOMErrorReporter () { }

	void warning (const xercesc::SAXParseException & e);
	void error (const xercesc::SAXParseException & e);
	void fatalError (const xercesc::SAXParseException & e);
	inline void resetErrors () { sawErrors = false; }
	inline bool getSawErrors () const { return sawErrors; }

};

#endif

-- code ends --

xmldomerrorreporter.cpp
-- code starts --

#include "xmldomerrorreporter.hpp"

void XMLDOMErrorReporter::warning (const xercesc::SAXParseException & e)
{

	char * tmp;

	std::cerr << "-- WARNING [DOM Parser] --" << std::endl;

	tmp = xercesc::XMLString::transcode (e.getMessage ());
	std::cerr << "Message: " << tmp << std::endl;
	xercesc::XMLString::release (&tmp);

	tmp = xercesc::XMLString::transcode (e.getSystemId ());
	std::cerr << "File: " << tmp << std::endl;
	xercesc::XMLString::release (&tmp);

	std::cerr << "Line: " << e.getLineNumber () << std::endl;

	std::cerr << "Column: " << e.getColumnNumber () << std::endl;

}

void XMLDOMErrorReporter::error (const xercesc::SAXParseException & e)
{

	sawErrors = true;
	char * tmp;

	std::cerr << "-- ERROR [DOM Parser] --" << std::endl;

	tmp = xercesc::XMLString::transcode (e.getMessage ());
	std::cerr << "Message: " << tmp << std::endl;
	xercesc::XMLString::release (&tmp);

	tmp = xercesc::XMLString::transcode (e.getSystemId ());
	std::cerr << "File: " << tmp << std::endl;
	xercesc::XMLString::release (&tmp);

	std::cerr << "Line: " << e.getLineNumber () << std::endl;

	std::cerr << "Column: " << e.getColumnNumber () << std::endl;

}

void XMLDOMErrorReporter::fatalError (const xercesc::SAXParseException & e)
{

	sawErrors = true;
	char * tmp;

	std::cerr << "-- FATAL ERROR [DOM Parser] --" << std::endl;

	tmp = xercesc::XMLString::transcode (e.getMessage ());
	std::cerr << "Message: " << tmp << std::endl;
	xercesc::XMLString::release (&tmp);

	tmp = xercesc::XMLString::transcode (e.getSystemId ());
	std::cerr << "File: " << tmp << std::endl;
	xercesc::XMLString::release (&tmp);

	std::cerr << "Line: " << e.getLineNumber () << std::endl;

	std::cerr << "Column: " << e.getColumnNumber () << std::endl;

}

-- code ends --

Best regards,

S.

-- 
question = ( to ) ? be : ! be;
 -- Wm. Shakespeare

Re: Validating XML with XSD and DOM Parser

Posted by Roberto Rigamonti <ro...@gmail.com>.
Hi, I had the same problem and I solved it in this way:

--- code starts ---
  XMLDOMErrorReporter* errReporter_;
  XercesDOMParser* parser_;

  errReporter_ = new XMLDOMErrorReporter();
  parser_->setErrorHandler(errReporter_);
  parser_->setValidationScheme(XercesDOMParser::Val_Always);
  parser_->setDoSchema(true);
  parser_->setDoNamespaces(true);
  parser_->setDoValidation(true);
  parser_->setValidationSchemaFullChecking(true);
  parser_->setIncludeIgnorableWhitespace(false);
--- code ends ---

where the XMLDOMErrorReporter object inherits from ErrorHandler, for
example in my case:

--- code starts ---
class XMLDOMErrorReporter : public ErrorHandler {
private:
  bool sawErrors;

public:
  XMLDOMErrorReporter() : sawErrors(false) { }
  ~XMLDOMErrorReporter() { }

  void
  warning(const SAXParseException& e)
  {
    std::cerr << "-- WARNING [DOM Parser] --" << std::endl;
    std::cerr << "message: " << XMLString::transcode(e.getMessage()) <<
std::endl;
    std::cerr << "file: " << XMLString::transcode(e.getSystemId()) <<
std::endl;
    std::cerr << "line: " << e.getLineNumber() << std::endl;
    std::cerr << "column: " << e.getColumnNumber() << std::endl;
  }

  void
  error(const SAXParseException& e)
  {
    sawErrors = true;
    std::cerr << "-- ERROR [DOM Parser] --" << std::endl;
    std::cerr << "message: " << XMLString::transcode(e.getMessage()) <<
std::endl;
    std::cerr << "file: " << XMLString::transcode(e.getSystemId()) <<
std::endl;
    std::cerr << "line: " << e.getLineNumber() << std::endl;
    std::cerr << "column: " << e.getColumnNumber() << std::endl;
  }

  void
  fatalError(const SAXParseException& e)
  {
    sawErrors = true;
    std::cerr << "-- FATAL ERROR [DOM Parser] --" << std::endl;
    std::cerr << "message: " << XMLString::transcode(e.getMessage()) <<
std::endl;
    std::cerr << "file: " << XMLString::transcode(e.getSystemId()) <<
std::endl;
    std::cerr << "line: " << e.getLineNumber() << std::endl;
    std::cerr << "column: " << e.getColumnNumber() << std::endl;
  }

  void
  resetErrors()
  {
    sawErrors = false;
  }

  bool getSawErrors() const
  {
    return sawErrors;
  }
};
--- code ends ---

The XSD file that should be used for validation in specified in the XML
instance, for example:

--- code starts ---
<?xml version="1.0" ?>
<visor xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:noNamespaceSchemaLocation="visor.xsd">
...
</visor>
--- code ends ---

Hope this helps!
Best regards.
                           Roberto

Sebastian 'CrashandDie' Lauwers wrote:
> Hi everyone,
> 
> I'm trying to validate get the DOM parser to validate an XML instance
> through an XSD file, though, I'm not really sure as to how to do
> this...
> 
> I've Googled quite a bit, but I think I'm looking in the wrong direction.
> 
> XSD file: http://slexy.org/view/s2iGvEBQAU
> Relevant code: http://slexy.org/view/s2c2p1D8HG
> 
> Could you point me in the right direction ? Any help is appreciated, really.
> 
> Best regards,
> 
> S.
>