BDE 4.14.0 Production release
|
Macros | |
#define | BALXML_DECODER_LOG_ERROR(reporter) |
#define | BALXML_DECODER_LOG_WARNING(reporter) |
#define | BALXML_DECODER_LOG_END |
Functions | |
balxml::Decoder::MemOutStream::MemOutStream (bslma::Allocator *basicAllocator=0) | |
void | balxml::Decoder::MemOutStream::reset () |
Reset the internal streambuf to empty. | |
const char * | balxml::Decoder::MemOutStream::data () const |
int | balxml::Decoder::MemOutStream::length () const |
Provide a generic translation from XML into C++ objects.
This component provides a class balxml::Decoder
for decoding value-semantic objects in XML format. The decode
methods are function templates that will decode any object that meets the requirements of a sequence or choice object as defined in the bdlat_sequencefunctions and bdlat_choicefunctions components. These generic frameworks provide a common compile-time interface for manipulating struct-like and union-like objects.
There are two usage models for using balxml::Decoder
. The common case, when the type of object being decoded is known in advance, involves calling one of a set of decode
method templates that decode a specified value-semantic object from a specified stream or other input source. The caller may specify the input for decode
as a file, an bsl::istream
, an bsl::streambuf
, or a memory buffer.
A less common but more flexible usage model involves calling the open
to open the XML document from the specified input, then calling decode
to decode to an object without specifying the input source, and finally calling close
to close the input source. The open
method positions the internal reader to the root element node, so the caller can examine the root element, decide what type of object is contained in the input stream/source, and construct an object of the needed type before calling decode
to read from the already open input source. Thus the input data is not constrained to a single root element type.
Although the XML format is very useful for debugging and for conforming to external data-interchange specifications, it is relatively expensive to encode and decode and relatively bulky to transmit. It is more efficient to use a binary encoding (such as BER) if the encoding format is under your control. (See balber_berdecoder .)
This section illustrates intended use of this component.
Suppose we have the following XML schema inside a file called employee.xsd
:
Using the bas_codegen.pl
tool, we can generate C++ classes for this schema:
This tool will generate the header and implementation files for the test_address and test_employee components in the current directory.
The following function decodes an XML string into a test::Employee
object and verifies the results:
The following snippets of code illustrate how to pass an error stream and warning stream to the decode
function. We will use the same test_employee component from the previous usage example. Note that the input XML string contains an error. (The homeAddress
object has an element called country
, which does not exist in the schema.):
Note that the input stream is invalidated to indicate that an error occurred. Also note that the following error message will be printed on bsl::cerr
:
The following snippets of code illustrate how to open decoder and read the first node before calling decode
:
Now we open the document, but we don't begin decoding yet:
Depending on the value of the first node, we can now determine whether the document is an Address
object or an Employee
object, and construct the target object accordingly:
When decoding is complete, we must close the decoder object:
#define BALXML_DECODER_LOG_END |
See usage of BALXML_DECODER_LOG_ERROR and BALXML_DECODER_LOG_WARNING, above.
#define BALXML_DECODER_LOG_ERROR | ( | reporter | ) |
Usage: BAEXML_LOG_ERROR(myDecoder) << "Message" << value << BALXML_DECODER_LOG_END;
#define BALXML_DECODER_LOG_WARNING | ( | reporter | ) |
Usage: BAEXML_LOG_WARNING(myDecoder) << "Message" << value << BALXML_DECODER_LOG_END;
|
inline |
Return a pointer to the memory containing the formatted values formatted to this stream. The data is not null-terminated unless a null character was appended onto this stream.
|
inline |
Return the length of the formatted data, including null characters appended to the stream, if any.
|
inline |
Create a new stream using the optionally specified basicAllocator
.
|
inline |