Skip Headers

Oracle9i XML Developer's Kits Guide - XDK
Release 2 (9.2)

Part Number A96621-01
Go To Documentation Library
Home
Go To Product List
Book List
Go To Table Of Contents
Contents
Go To Index
Index

Master Index

Feedback

Go to previous page Go to next page

19
XML Class Generator for C++

This chapter contains the following sections:

Accessing XML C++ Class Generator

The XML C++ Class Generator is provided with Oracle9i and is also available for download from the OTN site:

 http://otn.oracle.com/tech/xml

It is located in $ORACLE_HOME/xdk/cpp/classgen. Information about using the Class Generator is available with the software.

Using XML C++ Class Generator

The XML C++ Class Generator creates source files from an XML DTD or XML Schema. The Class Generator takes the Document Type Definition (DTD) or the XML Schema, and generates classes for each defined element. Those classes are then used in a C++ program to construct XML documents conforming to the DTD.

This is useful when an application wants to send an XML message to another application based on an agreed-upon DTD or XML Schema, or as the back end of a web form to construct an XML document. Using these classes, C++ applications can construct, validate, and print XML documents that comply with the input.

The Class Generator works in conjunction with the Oracle XML Parser for C++, which parses the input and passes the parsed document to the class generator.

External DTD Parsing

The XML C++ Class Generator can also parse an external DTD directly without requiring a complete (dummy) document by using the Oracle XML Parser for C++ routine xmlparsedtd().

The provided command-line program xmlcg has a '-d' option that is used to parse external DTDs. See "xmlcg Usage" .

Error Message Files

Error message files are provided in the mesg/ subdirectory. The messages files also exist in the $ORACLE_HOME/xdk/mesg directory. You may set the environment variable ORA_XML_MESG to point to the absolute path of the mesg/ subdirectory although this not required.

XML C++ Class Generator Usage

Figure 19-1 summarizes the XML C++ Class Generator usage.

  1. From the bin directory, at the command line, enter the following:
    xml [XML document file name, such as xxxxx]
    
    

    where XML document file name is the name of the parsed XML document or parsed DTD being processed. The XML document must have an associated DTD.

    The Input to the XML C++ Class Generator is an XML document containing a DTD, or an external DTD. The document body itself is ignored; only the DTD is relevant, though the document must conform to the DTD.

    Accepted character set encoding for input files are listed in "Input to the XML C++ Class Generator".

  2. Two source files are output, a xxxxx.h header file and a xxxxx.cpp C++ file. These are named after the DTD file.
  3. The output files are typically used to generate XML documents.

Constructors are provided for each class (element) that allow an object to be created in the following two ways:

A method is provided for #PCDATA (and Mixed) elements to set the data and, when appropriate, set an element's attributes.

Input to the XML C++ Class Generator

Input is an XML document containing a DTD. The document body itself is ignored; only the DTD is relevant, though the dummy document must conform to the DTD. The underlying XML parser only accepts file names for the document and associated external entities. In future releases, no dummy document will be required, and URIs for additional protocols will be accepted.

Character Set Support

The following lists supported Character Set Encoding for files input to XML C++ Class Generator. These are in addition to the character sets specified in Appendix A, "Character Sets", of Oracle9i Database Globalization Support Guide.

Default:

The default encoding is UTF-8. It is recommended that you set the default encoding explicitly if using only single byte character sets (such as US-ASCII or any of the ISO-8859 character sets) for performance up to 25% faster than with multibyte character sets, such as UTF-8.

Figure 19-1 XML C++ Class Generator Functionality

Text description of adxml048.gif follows
Text description of the illustration adxml048.gif


xmlcg Usage

The standalone parser may be called as an executable by invoking bin/xmlcg. For example:

xmlcg [flags] <XML document or External DTD>

Table 19-1 lists the xmlcg optional flags.

Table 19-1 xmlcg Optional Flags  
xmlcg Optional Flags Description

-d name

DTD - Input is an external DTD with the given name

-o directory

Output directory for generated files (default is current directory)

-e encoding

Encoding - Default input file encoding

-h

Help - Show this usage help

-v

Version - Show the Class Generator version

Using the XML C++ Class Generator Examples in sample

Table 19-2 lists the files supplied the sample XML C++ Class Generator sample directory.

Table 19-2 XML C++ Class Generator Examples in sample/
Sample File Name Description

CG.cpp

Sample program

CG.xml

XML file contains DTD and dummy document

CG.dtd

DTD file referenced by CG.xml

Make.bat on Windows NT

Makefile on UNIX

Batch file (on Windows NT) or script file (on UNIX) to generate classes and build the sample programs.

README

A readme file with these instructions

The make.bat batch file (on Windows NT) or Makefile (on UNIX) do the following:

XML C++ Class Generator Example 1: XML -- Input File to Class Generator, CG.xml

This XML file, CG.xml, inputs XML C++ Class Generator. It references the DTD file, CG.dtd.

<?xml version="1.0"?>
<!DOCTYPE Sample SYSTEM "CG.dtd">
  <Sample>
    <B>Be!</B>
    <D attr="value"></D>
    <E>
      <F>Formula1</F>
      <F>Formula2</F>
    </E>
  </Sample>

XML C++ Class Generator Example 2: DTD -- Input File to Class Generator, CG.dtd

This DTD file, CG.dtd is referenced by the XML file CG.xml. CG.xml inputs XML C++ Class Generator.

<!ELEMENT Sample (A | (B, (C | (D, E))) | F)>
<!ELEMENT A (#PCDATA)>
<!ELEMENT B (#PCDATA | F)*>
<!ELEMENT C (#PCDATA)>
<!ELEMENT D (#PCDATA)>
<!ATTLIST D attr CDATA #REQUIRED>
<!ELEMENT E (F, F)>
<!ELEMENT F (#PCDATA)>

XML C++ Class Generator Example 3: CG Sample Program

The CG sample program, CG.cpp, does the following:

  1. Initializes the XML parser
  2. Loads the DTD (by parsing the DTD-containing file-- the dummy document part is ignored)
  3. Creates some objects using the generated classes
  4. Invokes the validation function which verifies that the constructed classes match the DTD
  5. Writes the constructed document to Sample.xml
//////////////////////////////////////////////////////////////////////////////
// NAME        CG.cpp
// DESCRIPTION Demonstration program for C++ Class Generator usage
//////////////////////////////////////////////////////////////////////////////

#ifndef ORAXMLDOM_ORACLE
# include <oraxmldom.h>
#endif

#include <fstream.h>

#include "Sample.h"

#define DTD_DOCUMENT  "CG.xml"
#define OUT_DOCUMENT  "Sample.xml"

int main()
{
    XMLParser parser;
    Document *doc;
    Sample   *samp;
    B        *b;
    D        *d;
    E        *e;
    F        *f1, *f2;
    fstream  *out;
    ub4       flags = XML_FLAG_VALIDATE;
    uword     ecode;

    // Initialize XML parser
    cout << "Initializing XML parser...\n";
    if (ecode = parser.xmlinit())
    {
cout << "Failed to initialize parser, code " << ecode << "\n";
        return 1;
    }

    // Parse the document containing a DTD; parsing just a DTD is not
    // possible yet, so the file must contain a valid document (which
    // is parsed but we're ignoring).
    cout << "Loading DTD from " << DTD_DOCUMENT << "...\n";
    if (ecode = parser.xmlparse((oratext *) DTD_DOCUMENT, (oratext *)0, flags))
    {
cout << "Failed to parse DTD document " << DTD_DOCUMENT <<
    ", code " << ecode << "\n";
return 2;
    }

    // Fetch dummy document
    cout << "Fetching dummy document...\n";
    doc = parser.getDocument();

    // Create the constituent parts of a Sample
    cout << "Creating components...\n";
    b = new B(doc, (String) "Be there or be square");
    d = new D(doc, (String) "Dit dah");
    d->setattr((String) "attribute value");
    f1 = new F(doc, (String) "Formula1");
    f2 = new F(doc, (String) "Formula2");
    e = new E(doc, f1, f2);

    // Create the Sample
    cout << "Creating top-level element...\n";
    samp = new Sample(doc, b, d, e);

    // Validate the construct
    cout << "Validating...\n";
    if (ecode = parser.validate(samp))
    {
  cout << "Validation failed, code " << ecode << "\n";
  return 3;
    }

    // Write out doc
    cout << "Writing document to " << OUT_DOCUMENT << "\n";
    if (!(out = new fstream(OUT_DOCUMENT, ios::out)))
    {
  cout << "Failed to open output stream\n";
  return 4;
    }
    samp->print(out, 0);
    out->close();

    // Everything's OK
    cout << "Success.\n";

    // Shut down
    parser.xmlterm();
    return 0;
}

// end of CG.cpp


Go to previous page Go to next page
Oracle
Copyright © 2001, 2002 Oracle Corporation.

All Rights Reserved.
Go To Documentation Library
Home
Go To Product List
Book List
Go To Table Of Contents
Contents
Go To Index
Index

Master Index

Feedback