Skip Headers

Oracle® XML Reference 10g (9.0.4) Part Number B10926-01 |
Home | Contents | Index

# 17
Oracle XML Class Generator (C++)

This chapter details the XML Class Generator for C++ and how it defines each element.

The XML Class Generator takes a Document Type Definition (DTD) and generates classes for each defined element. Those classes are then used in a C++ program to construct XML documents conforming to the DTD. Supported operating systems are Solaris 2.6, Linux 2.2, and NT 4 (Service Pack 3 and above).

This chapter contains the following sections:

- Using Class Generator for C++

- Class XMLClassGenerator

- Class generated

  See Also: Oracle Application Developer's Guide - XML

## Using Class Generator for C++

Input is an XML document containing a DTD. The document body itself is ignored; only the DTD is relevant, though the document must conform to the DTD. The underlying XML parser only accepts file names for the document and associated external entities. The supported input file encodings include UTF-8, UTF-16, US-ASCII, ISO-10646-UCS-2, ISO-8859-1, ISO-8859-2, ISO-8859-3, ISO-8859-4, ISO-8859-5, ISO-8859-6, ISO-8859-7, ISO-8859-8, ISO-8859-9, EUC-JP, SHIFT_JIS, BIG5, GB2312, KOI8-R, EBCDIC-CP-US, EBCDIC-CP-CA, EBCDIC-CP-NL, EBCDIC-CP-WT, EBCDIC-CP-DK, EBCDIC-CP-NO, EBCDIC-CP-FI, EBCDIC-CP-SE, EBCDIC-CP-IT, EBCDIC-CP-ES, EBCDIC-CP-GB, EBCDIC-CP-FR, EBCDIC-CP-HE, EBCDIC-CP-BE, EBCDIC-CP-CH, EBCDIC-CP-ROECE, EBCDIC-CP-YU, and EBCDIC-CP-IS.

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) to improve performance as much as 25% over multibyte character sets such as UTF-8.

Output is a pair of C++ source files, .cpp and .h, named after the DTD. Constructors are provided for each class (element) that allow an object to be created in two different ways: initially empty, then adding the children or data after the initial creation, or created with the initial full set of children or initial data. A method is provided for #PCDATA (and Mixed) elements to set the data and, when appropriate, set an element's attributes.

Relevant standards include the W3C recommendation for Extensible Markup Language (XML) 1.0, Document Object Model Level 1 1.0, Namespaces in XML, and the simple API for XML (SAX) 1.0.

#### Example

The standalone parser may be called as an executable by invoking `bin/xmlcg like xmlcg [flags] &lt;XML document&gt;.` The optional flags are listed in Table 17-1.

##### Table 17-1 Optional Flags

Flag | Description
--- | ---
    -d
| Specify output directory; default is current directory)
    -e
| Specify default input file encoding
    -h
| Show help instructions

## Class XMLClassGenerator

Generates classes based on a DTD.

## generate()

Generates classes for the given DTD. Two files are created in the output directory outdir (or in the current directory if outdir is NULL): DTDname.h and DTDname.cpp, both named after the DTD. One class is generated for each defined element in the DTD. Returns `uword` error code, `0` on success

#### Syntax

    uword generate(DocumentType *dtd, char *outdir)

Parameter | Description
--- | ---
    dtd
| DTD source used to generate the classes
    outdir
| output directory for generated files

## Class generated

A generated class is produced for each element defined in the DTD. It has the same name as the element.

Constructors are provided which create an empty element, or make it with an initial set of children or data. Methods are provided to add children or data after construction, and to set attributes. There are two styles of creation: make an empty element, then add the children one at a time, or construct the element with initial data or children. For example, given the element declaration `<!ELEMENT B (#PCDATA | F)*>`, the following constructors will be provided:

    B(Document *doc);
    B(Document *doc, String data);
    B(Document *doc, F *theF);

The first constructor just makes an empty element with no children. The second initializes it with PCDATA, and the third with a single child node of element F. An element like B which may contain PCDATA is also given a method to add the data post-construction:

    void addData(Document *doc, String data);

The following usages are equivalent:

    b = new B("data");

and

    b = new B();
    b->addData("data");

Similarly, the following are also equivalent:

    f = new F(...);
    b = new B(f);

and

    f = new F(...);
    b = new B();
    b->addNode(f);

The presence of modifiers '?' (optional), '*' (zero or more), and '+' (one or more) is ignored when forming the constructors. For example, for the element `<!ELEMENT Sample (A* | (B, (C? | (D, E)*)) | F)+>,` the following constructors are made as if the modifiers were not present:

    Sample(Document *doc);
    Sample(Document *doc, A *theA);
    Sample(Document *doc, B *theB, C *theC);
    Sample(Document *doc, B *theB, D *theD, E *theE);
    Sample(Document *doc, F *theF);

If you cannot make the desired final element using one of the forms which take initial children, you must start with the empty element and add nodes as needed with `addNode` as above.

For each attribute for an element, a method is provided to set its value. For example, for the element declaration `<!ELEMENT D (#PCDATA)> ... <!ATTLIST D foo CDATA #REQUIRED>`, the class D will have the method `Attr* setfoo(String value)`.The constructed element is not tested for validity as it is being made. The user to explicitly call the `XMLParser's` validate method on the final element.

##### Table 17-2 Summary of Methods of generated

Method | Description
--- | ---
Constructor() | Constructs an element that belongs to the document.
addData() | Adds data to the element.
addNode() | Appends a child node to the element.
setattribute() | Sets the element's attribute value.

## Constructor()

Constructs an element that belongs to the document. Form one makes the element with no children; use `addData` and addNode as appropriate to populate it. Form two provides initial data or children, depending on the element definition.

#### Syntax

    class(Document *doc)
    class(Document *doc, ...)

Parameter | Description
--- | ---
doc | document to which the element belongs
... | list of arguments; these depends on the element definition

## addData()

Adds data to the element. That is, appends to it a PCDATA subnode with the given value. If multiple addData calls are made, the node will have multiple PCDATA subnodes, which should be normalized when construction is finished.

#### Syntax

    void addData(Document *doc, String data)

Parameter | Description
--- | ---
doc | document to which the element belongs
data | data to be added

## addNode()

Appends a child node to the element. No effort is made to validate the resulting element structure at this time; it is the user's responsibility to form the element properly, which may be verified with `XMLParser::validate`.

#### Syntax

    void addNode(node thenode)

Parameter | Description
--- | ---
node | Node added

## setattribute()

Sets the element's attribute value. One method is provided for each attribute, named after the attribute as setattribute. Returns the created attribute.

#### Syntax

    Attr* setattribute(String value)

Parameter | Description
--- | ---
value | the attribute's value

| Copyright © 2001, 2003 Oracle Corporation. All Rights Reserved. |
Home | Contents | Index