blob: 7e733b03066ddfcb739238bfc977f7677264d945 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
// Necessary includes. We refer to these as "common includes"
// in the following examples.
#include <xercesc/sax2/XMLReaderFactory.hpp>
#include <xercesc/sax2/SAX2XMLReader.hpp>
#include <xercesc/sax2/DefaultHandler.hpp>
// Handy definitions of constants.
#include <xercesc/util/XMLUni.hpp>
#include <iostream>
using namespace std;
XERCES_CPP_NAMESPACE_USE
class Handler : public DefaultHandler
{
public:
virtual void error (const SAXParseException &exc) {
char* message = XMLString::transcode(exc.getMessage());
cout << "Exception: " << message << "\n";
XMLString::release(&message);
}
virtual void fatalError (const SAXParseException &exc) {
char* message = XMLString::transcode(exc.getMessage());
cout << "Exception: " << message << "\n";
XMLString::release(&message);
}
};
int main(int argc, char* argv[])
{
XMLPlatformUtils::Initialize();
// Create a SAX2 parser object.
SAX2XMLReader* parser = XMLReaderFactory::createXMLReader();
// Set the appropriate features on the parser.
// Enable namespaces, schema validation, and the checking
// of all Schema constraints.
// We refer to these as "common features" in following examples.
parser->setFeature(XMLUni::fgSAX2CoreNameSpaces, true);
parser->setFeature(XMLUni::fgSAX2CoreValidation, true);
parser->setFeature(XMLUni::fgXercesDynamic, false);
parser->setFeature(XMLUni::fgXercesSchema, true);
parser->setFeature(XMLUni::fgXercesSchemaFullChecking, true);
//parser->setProperty(XMLUni::fgXercesSchemaExternalNoNameSpaceSchemaLocation, (void *)"pic.xsd");
// Set appropriate ContentHandler, ErrorHandler, and EntityResolver.
// These will be referred to as "common handlers" in subsequent examples.
// You will use a default handler provided by Xerces-C++ (no op action).
// Users should write their own handlers and install them.
Handler handler;
parser->setContentHandler(&handler);
// The object parser calls when it detects violations of the schema.
parser->setErrorHandler(&handler);
// The object parser calls to find the schema and
// resolve schema imports/includes.
parser->setEntityResolver(&handler);
// Parse the XML document.
// Document content sent to registered ContentHandler instance.
if ( argc==1 ) { printf("Needs one argument\n"); return -1; }
parser->parse(argv[1]);
// Delete the parser instance.
delete parser;
return 0;
}
|