You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by tn...@apache.org on 2002/05/21 21:15:51 UTC
cvs commit: xml-xerces/c/samples/DOMCount DOMCount.cpp
tng 02/05/21 12:15:51
Modified: c/samples/DOMCount DOMCount.cpp
Log:
Samples Update: Modify to use the new DOM interface.
Revision Changes Path
1.24 +65 -23 xml-xerces/c/samples/DOMCount/DOMCount.cpp
Index: DOMCount.cpp
===================================================================
RCS file: /home/cvs/xml-xerces/c/samples/DOMCount/DOMCount.cpp,v
retrieving revision 1.23
retrieving revision 1.24
diff -u -r1.23 -r1.24
--- DOMCount.cpp 17 Apr 2002 20:18:08 -0000 1.23
+++ DOMCount.cpp 21 May 2002 19:15:51 -0000 1.24
@@ -1,7 +1,7 @@
/*
* The Apache Software License, Version 1.1
*
- * Copyright (c) 1999-2001 The Apache Software Foundation. All rights
+ * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -55,7 +55,7 @@
*/
/*
- * $Id: DOMCount.cpp,v 1.23 2002/04/17 20:18:08 tng Exp $
+ * $Id: DOMCount.cpp,v 1.24 2002/05/21 19:15:51 tng Exp $
*/
// ---------------------------------------------------------------------------
@@ -64,8 +64,10 @@
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/sax/SAXException.hpp>
#include <xercesc/sax/SAXParseException.hpp>
-#include <xercesc/parsers/DOMParser.hpp>
-#include <xercesc/dom/DOM_DOMException.hpp>
+#include <xercesc/parsers/XercesDOMParser.hpp>
+#include <xercesc/dom/DOMException.hpp>
+#include <xercesc/dom/DOMDocument.hpp>
+#include <xercesc/dom/DOMNodeList.hpp>
#include "DOMCount.hpp"
#include <string.h>
#include <stdlib.h>
@@ -73,12 +75,13 @@
+
// ---------------------------------------------------------------------------
// This is a simple program which invokes the DOMParser to build a DOM
// tree for the specified input file. It then walks the tree and counts
// the number of elements. The element count is then printed.
// ---------------------------------------------------------------------------
-void usage()
+static void usage()
{
cout << "\nUsage:\n"
" DOMCount [options] <XML file | List file>\n\n"
@@ -97,6 +100,31 @@
}
+
+// ---------------------------------------------------------------------------
+//
+// Recursively Count up the total number of child Elements under the specified Node.
+//
+// ---------------------------------------------------------------------------
+static int countChildElements(DOMNode *n)
+{
+ DOMNode *child;
+ int count = 0;
+ if (n) {
+ if (n->getNodeType() == DOMNode::ELEMENT_NODE)
+ count++;
+ for (child = n->getFirstChild(); child != 0; child=child->getNextSibling())
+ count += countChildElements(child);
+ }
+ return count;
+}
+
+
+// ---------------------------------------------------------------------------
+//
+// main
+//
+// ---------------------------------------------------------------------------
int main(int argC, char* argV[])
{
// Initialize the XML4C system
@@ -120,13 +148,13 @@
return 1;
}
- const char* xmlFile = 0;
- DOMParser::ValSchemes valScheme = DOMParser::Val_Auto;
- bool doNamespaces = false;
- bool doSchema = false;
- bool schemaFullChecking = false;
- bool doList = false;
- bool errorOccurred = false;
+ const char* xmlFile = 0;
+ XercesDOMParser::ValSchemes valScheme = XercesDOMParser::Val_Auto;
+ bool doNamespaces = false;
+ bool doSchema = false;
+ bool schemaFullChecking = false;
+ bool doList = false;
+ bool errorOccurred = false;
int argInd;
for (argInd = 1; argInd < argC; argInd++)
@@ -148,14 +176,15 @@
const char* const parm = &argV[argInd][3];
if (!strcmp(parm, "never"))
- valScheme = DOMParser::Val_Never;
+ valScheme = XercesDOMParser::Val_Never;
else if (!strcmp(parm, "auto"))
- valScheme = DOMParser::Val_Auto;
+ valScheme = XercesDOMParser::Val_Auto;
else if (!strcmp(parm, "always"))
- valScheme = DOMParser::Val_Always;
+ valScheme = XercesDOMParser::Val_Always;
else
{
cerr << "Unknown -v= value: " << parm << endl;
+ XMLPlatformUtils::Terminate();
return 2;
}
}
@@ -193,11 +222,12 @@
if (argInd != argC - 1)
{
usage();
+ XMLPlatformUtils::Terminate();
return 1;
}
// Instantiate the DOM parser.
- DOMParser* parser = new DOMParser;
+ XercesDOMParser* parser = new XercesDOMParser;
parser->setValidationScheme(valScheme);
parser->setDoNamespaces(doNamespaces);
parser->setDoSchema(doSchema);
@@ -250,6 +280,7 @@
try
{
const unsigned long startMillis = XMLPlatformUtils::getCurrentMillis();
+ parser->resetDocumentPool();
parser->parse(xmlFile);
const unsigned long endMillis = XMLPlatformUtils::getCurrentMillis();
duration = endMillis - startMillis;
@@ -263,7 +294,7 @@
errorOccurred = true;
continue;
}
- catch (const DOM_DOMException& toCatch)
+ catch (const DOMException& toCatch)
{
cerr << "\nDOM Error during parsing: '" << xmlFile << "'\n"
<< "DOMException code is: \n"
@@ -289,8 +320,17 @@
}
else
{
- DOM_Document doc = parser->getDocument();
- unsigned int elementCount = doc.getElementsByTagName("*").getLength();
+ DOMDocument *doc = parser->getDocument();
+ unsigned int elementCount = 0;
+ if (doc) {
+ elementCount = countChildElements((DOMNode*)doc->getDocumentElement());
+ // test getElementsByTagName and getLength
+ XMLCh xa[] = {chAsterisk, chNull};
+ if (elementCount != doc->getElementsByTagName(xa)->getLength()) {
+ cout << "\nErrors occurred, element count is wrong\n" << endl;
+ errorOccurred = true;
+ }
+ }
// Print out the stats that we collected and time taken.
cout << xmlFile << ": " << duration << " ms ("
@@ -298,9 +338,6 @@
}
}
- if (doList)
- fin.close();
-
//
// Delete the parser itself. Must be done prior to calling Terminate, below.
//
@@ -309,12 +346,17 @@
// And call the termination method
XMLPlatformUtils::Terminate();
+ if (doList)
+ fin.close();
+
if (errorOccurred)
return 4;
else
return 0;
-
}
+
+
+
DOMCountErrorHandler::DOMCountErrorHandler() :
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-cvs-help@xml.apache.org