Oracle8i Application Developer's Guide - XML Release 3 (8.1.7) Part Number A86030-01 |
Library | Service | Contents | Index

Using XML C++ Class Generator, 6 of 6

## Using the XML C++ Class Generator Examples in sample/

Table 23-2 lists the files in the sample XML C++ Class Generator sample/ directory.

##### Table 23-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:

- Generate classes based on CG.xml into Sample.h and Sample.cpp

- Compile the program CG.cpp (using Sample.h), and link this with the Sample object into an executable named CG.exe in the ...\bin (or .../bin) directory.

### 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

| Copyright © 1996-2000, Oracle Corporation. All Rights Reserved. |
Library | Service | Contents | Index