http://xml.apache.org/http://www.apache.org/http://www.w3.org/

Home

Readme
Release Info

Installation
Download
Build Instructions

FAQs
Samples
API Docs

DOM C++ Binding
Programming
Migration Guide

Feedback
Bug-Reporting
PDF Document

CVS Repository
Mail Archive

Version Macro
 

Xerces-C++ has defined a numeric preprocessor macro, _XERCES_VERSION, for users to introduce into their code to perform conditional compilation where the version of Xerces is detected in order to enable or disable version specific capabilities. For example,

       #if _XERCES_VERSION >= 20304
         // code specific to Xerces-C++ version 2.3.4
       #else
         // old code here...
       #endif

The minor and revision (patch level) numbers have two digits of resolution which means that '3' becomes '03' and '4' becomes '04' in this example.

There are also other string macro, or constants to represent the Xerces-C++ version. Please refer to the header xercesc/util/XercesVersion.hpp for further details.


Schema Support
 

Xerces-C++ contains an implementation of the W3C XML Schema Language. See the Schema page for details.


Progressive Parsing
 

In addition to using the parse() method to parse an XML File. You can use the other two parsing methods, parseFirst() and parseNext() to do 'progressive parsing', so that you don't have to depend upon throwing an exception to terminate the parsing operation.

Calling parseFirst() will cause the DTD (both internal and external subsets), and any pre-content, i.e. everything up to but not including the root element, to be parsed. Subsequent calls to parseNext() will cause one more pieces of markup to be parsed, and spit out from the core scanning code to the parser (and hence either on to you if using SAX or into the DOM tree if using DOM).

You can quit the parse any time by just not calling parseNext() anymore and breaking out of the loop. When you call parseNext() and the end of the root element is the next piece of markup, the parser will continue on to the end of the file and return false, to let you know that the parse is done. So a typical progressive parse loop will look like this:

// Create a progressive scan token
XMLPScanToken token;

if (!parser.parseFirst(xmlFile, token))
{
  cerr << "scanFirst() failed\n" << endl;
  return 1;
}

//
// We started ok, so lets call scanNext()
// until we find what we want or hit the end.
//
bool gotMore = true;
while (gotMore && !handler.getDone())
  gotMore = parser.parseNext(token);

In this case, our event handler object (named 'handler' surprisingly enough) is watching form some criteria and will return a status from its getDone() method. Since the handler sees the SAX events coming out of the SAXParser, it can tell when it finds what it wants. So we loop until we get no more data or our handler indicates that it saw what it wanted to see.

When doing non-progressive parses, the parser can easily know when the parse is complete and insure that any used resources are cleaned up. Even in the case of a fatal parsing error, it can clean up all per-parse resources. However, when progressive parsing is done, the client code doing the parse loop might choose to stop the parse before the end of the primary file is reached. In such cases, the parser will not know that the parse has ended, so any resources will not be reclaimed until the parser is destroyed or another parse is started.

This might not seem like such a bad thing; however, in this case, the files and sockets which were opened in order to parse the referenced XML entities will remain open. This could cause serious problems. Therefore, you should destroy the parser instance in such cases, or restart another parse immediately. In a future release, a reset method will be provided to do this more cleanly.

Also note that you must create a scan token and pass it back in on each call. This insures that things don't get done out of sequence. When you call parseFirst() or parse(), any previous scan tokens are invalidated and will cause an error if used again. This prevents incorrect mixed use of the two different parsing schemes or incorrect calls to parseNext().


Preparsing Grammar and Grammar Caching
 

Xerces-C++ 2.1.0 provides a new function to pre-parse the grammar so that users can check for any syntax or error before using the grammar. Users can also optionally cache these pre-parsed grammars for later use during actual parsing.

Here is an example:

XercesDOMParser parser;

// enbale schema processing
parser.setDoSchema(true);
parser.setDONamespaces(true);

// Let's preparse the schema grammar (.xsd) and cache it.
Grammar* grammar = parser.loadGrammar(xmlFile, Grammar::SchemaGrammarType, true);

Besides caching pre-parsed schema grammars, users can also cache any grammars encountered during an xml document parse.

Here is an example:

SAXParser parser;

// Enable grammar caching by setting cacheGrammarFromParse to true.
// The parser will cache any encountered grammars if it does not
// exist in the pool.
// If the grammar is DTD, no internal subset is allowed.
parser.cacheGrammarFromParse(true);

// Let's parse our xml file (DTD grammar)
parser.parse(xmlFile);

// We can get the grammar where the root element was declared
// by calling the parser's method getRootGrammar;
// Note: The parser owns the grammar, and the user should not delete it.
Grammar* grammar = parser.getRootGrammar();

We can use any previously cached grammars when parsing new xml documents. Here are some examples on how to use those cached grammars:

/**
  * Caching and reusing XML Schema (.xsd) grammar
  * Parse an XML document and cache its grammar set. Then,  use the cached
  * grammar set in subsequent parses.
  */

XercesDOMParser parser;

// Enable schema processing
parser.setDoSchema(true);
parser.setDoNamespaces(true);

// Enable grammar caching
parser.cacheGrammarFromParsing(true);

// Let's parse the XML document. The parser will cache any grammars encounterd.
parser.parse(xmlFile);

// No need to enable re-use by setting useCachedGrammarInParse to true. It is
// automatically enabled with grammar caching.
for (int i=0; i< 3; i++)
    parser.parse(xmlFile);

// This will flush the grammar pool
parser.resetCachedGrammarPool();
/**
  * Caching and reusing DTD grammar
  * Preparse a grammar and cache it in the pool. Then, we use the cached grammar
  * when parsing XML documents.
  */

SAX2XMLReader* parser = XMLReaderFactory::createXMLReader();

// Load grammar and cache it
parser->loadGrammar(dtdFile, Grammar::DTDGrammarType, true);

// enable grammar reuse
parser->setFeature(XMLUni::fgXercesUseCachedGrammarInParse, true);

// Parse xml files
parser->parse(xmlFile1);
parser->parse(xmlFile2);

There are some limitations about caching and using cached grammars:

  • When caching/reusing DTD grammars, no internal subset is allowed.
  • When preparsing grammars with caching option enabled, if a grammar, in the result set, already exists in the pool (same NS for schema or same system id for DTD), the entire set will not be cached.
  • When parsing an XML document with the grammar caching option enabled, the reuse option is also automatically enabled. We will only parse a grammar if it does not exist in the pool.

Loadable Message Text
 

The Xerces-C++ supports loadable message text. Although the current drop just supports English, it is capable to support other languages. Anyone interested in contributing any translations should contact us. This would be an extremely useful service.

In order to support the local message loading services, all the error messages are captured in an XML file in the src/xercesc/NLS/ directory. There is a simple program, in the Tools/NLSXlat/ directory, which can spit out that text in various formats. It currently supports a simple 'in memory' format (i.e. an array of strings), the Win32 resource format, and the message catalog format. The 'in memory' format is intended for very simple installations or for use when porting to a new platform (since you can use it until you can get your own local message loading support done.)

In the src/xercesc/util/ directory, there is an XMLMsgLoader class. This is an abstraction from which any number of message loading services can be derived. Your platform driver file can create whichever type of message loader it wants to use on that platform. Xerces-C++ currently has versions for the in memory format, the Win32 resource format, and the message catalog format. An ICU one is present but not implemented yet. Some of the platforms can support multiple message loaders, in which case a #define token is used to control which one is used. You can set this in your build projects to control the message loader type used.


Pluggable Transcoders
 

Xerces-C++ also supports pluggable transcoding services. The XMLTransService class is an abstract API that can be derived from, to support any desired transcoding service. XMLTranscoder is the abstract API for a particular instance of a transcoder for a particular encoding. The platform driver file decides what specific type of transcoder to use, which allows each platform to use its native transcoding services, or the ICU service if desired.

Implementations are provided for Win32 native services, ICU services, and the iconv services available on many Unix platforms. The Win32 version only provides native code page services, so it can only handle XML code in the intrinsic encodings ASCII, UTF-8, UTF-16 (Big/Small Endian), UCS4 (Big/Small Endian), EBCDIC code pages IBM037 and IBM1140 encodings, ISO-8859-1 (aka Latin1) and Windows-1252. The ICU version provides all of the encodings that ICU supports. The iconv version will support the encodings supported by the local system. You can use transcoders we provide or create your own if you feel ours are insufficient in some way, or if your platform requires an implementation that Xerces-C++ does not provide.



Copyright © 2001 The Apache Software Foundation. All Rights Reserved.