ARB
testParser.cxx
Go to the documentation of this file.
1 #include <xercesc/util/PlatformUtils.hpp>
2 #include <xercesc/util/TransService.hpp>
3 #include <xercesc/sax2/SAX2XMLReader.hpp>
4 #include <xercesc/sax2/XMLReaderFactory.hpp>
5 #include "testParser.hxx"
6 
7 
8 static void usage() {
9  cout << "\nUsage: myParser <xml_file> <out_put_file_name>"<< endl;
10 }
11 
12 int main(int argC, char* argV[]) {
13  const char* encodingName = "LATIN1";
14  const char* fileName = "example" ;
15  const char* xmlFile = 0;
16 
17  XMLFormatter::UnRepFlags unRepFlags = XMLFormatter::UnRep_CharRef;
18 
19  // Initialize the XML4C2 system
20  try {
22  }
23  catch (const XMLException& toCatch) {
24  cerr << "Error during initialization! :\n"
25  << StrX(toCatch.getMessage()) << endl;
26  return 1;
27  }
28 
29  // Check command line and extract arguments.
30  if (argC < 3) {
31  usage();
32  XMLPlatformUtils::Terminate();
33  return 1;
34  }
35 
36  int parmInd = 1;
37  if (parmInd < argC) {
38  xmlFile = argV[parmInd++]; // xml source file name
39  fileName = argV[parmInd]; // destination file name
40  }
41 
42  // Create a SAX parser object and set the validations
43 
44  SAX2XMLReader* parser = XMLReaderFactory::createXMLReader();
45 
46  // Create the handler object and install it as the document and error
47  // handler for the parser. Then parse the file and catch any exceptions
48  // that propagate out
49 
50  ofstream outFile;
51  outFile.open(fileName, ios::out); //open destination file to write
52 
53  int errorCount = 0;
54  try {
55  MySAXHandler myhandler(encodingName, unRepFlags, outFile);
56  parser->setContentHandler(&myhandler);
57  parser->setErrorHandler(&myhandler);
58  parser->parse(xmlFile);
59  errorCount = parser->getErrorCount();
60  outFile.close(); // close the file
61  }
62  catch (const XMLException& toCatch) {
63  cerr << "\nAn error occurred\n Error: "
64  << StrX(toCatch.getMessage())<< endl;
65  XMLPlatformUtils::Terminate();
66  return 4;
67  }
68 
69  // Delete the parser itself. Must be done prior to calling Terminate, below.
70 
71  delete parser;
72 
73  // And call the termination method
74  XMLPlatformUtils::Terminate();
75 
76  if (errorCount > 0)
77  return 4;
78  else
79  return 0;
80 }
81 
static void Initialize(GLwDrawingAreaWidget req, GLwDrawingAreaWidget neww, ArgList args, Cardinal *num_args)
Definition: GLwDrawA.c:417
static void usage()
Definition: testParser.cxx:8
int main(int argC, char *argV[])
Definition: testParser.cxx:12