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

Using XML Java Class Generator, 8 of 8

## Frequently Asked Questions (FAQs) : Class Generator for Java

This section lists XML Java Class Generator questions and answers.

### Automatic Population

#### Question

I used XML Java Class Generator to generate some class files for a sample DTD schema. On looking at the classes generated, I saw only APIs to construct instances through regular means (providing values to members) and APIs to print the XML representation to an outputstream. The functionality is not as complete as I expected it to be. I expected the document class to include a constructor that will take an XML document object and populate the various members automatically.

Does this functionality exist?

#### Answer

XML Java Class Generator does not have this functionality at this time as only the XML Parser for Java can build DOM trees.

### XML to Java Object Mapping

#### Question

I want to use XML as the storage medium for meta-data. This meta-data consists of complex, nested structures for which XML is well suited. At system start-up time I need to have access to all these meta-data objects as first class Java objects.However, I don't want to have to write custom code to run atop an XML parser such as Oracle XML Parser to build instances of these objects (one XML file -> one instance).

My ideal interface looks like this:

DTD/XML Schema + XML files fed to an XMLClassGenerator (runs atop an XMLParser) spits out Java source code that instantiates my meta-data object.

For example, if I have a meta.dtd (schema) and meta.xml which I want to map to a Meta.java.class Meta {public String metaName;public int numMeta;public Date dateMeta;};

I want to be able to run XMLClassGenerator on meta.dtd (schema) and meta.xml and get this kind of source code output:

    MetaBuilder.javaclass MetaBuilder {public init()
     {
        Meta metaInstance = new Meta();
        metaInstance.metaName = "Example1";
        metaInstance.numMeta = 1;
        metaInstance.dateMeta = new Date("07/16/1999");}

Do you plan to extend the Class Generator capability to do this? I can use this to send Java objects across a network, instead of an XML document over RMI.

#### Answer

I also am not aware of a direct solution out there. This would be an interesting extension for our Class Generator.

### DTD Class Generator: Which Child Classes Work

#### Question

In transcribing the code for the DTD Class Generator code form an article, I encountered an error I do not know how to correct. The program attempts to instantiate an XMLParser object, whose class is defined as abstract. What child classes of this class would work for running the code?

#### Answer

XMLParser was concrete in our V1 XML Parser. It's now abstract in V2 and you must either construct a DOMParser or a SAXParser. The article is probably using a DOMParser.

### Installing XML Class Generator

#### Question

What steps install XML Java Class Generator?

#### Answer

There are basically two steps:

1. Download the XML Class Generator for Java

2. Set CLASSPATH to include both classgen.jar from the /bin directory and xmlparserv2.jar from the XML Parser for Java v2 download.

### Role of the XML Class Generator for Java

#### Question 1

What does the XML Class Generator for Java do?

#### Answer 1

The XML Class Generator for Java creates Java source files from an XML DTD. This is useful when an application wants to send an XML message to another application based on an agreed-upon DTD or as the back end of a web form to construct and XML document. Using these classes, Java applications can construct, validate, and print XML documents that comply with the input DTD. The Class Generator works in conjunction with the Oracle XML Parser for Java v2, which parses the DTD and passes the parsed document to the class generator.

#### Question 2

How do I use the XML Class Generator for Java to get XML data?

#### Answer 2

First, get the data from the database using JDBC ResultSets. Then, instantiate objects using the classes generated by the XML Class Generator.

### Automatic Instantiation of Objects Based on XML File: Using the XML Class Generator

#### Question

Can I automatically instantiate objects based on an XML file?

#### Answer

Presently, you can only use DTD to generate classes and then instantiate each object by calling the create methods in the class. Please contact us if you have a business need to use XML file to instantiate objects automatically.

### Which DTD's are Supported?

#### Question

Does XML Java Class Generator support any kind of DTD?

#### Answer

Yes, it supports any kind of DTD that is XML1.0 compliant.

### Using the XML Class Generator Samples

#### Question

How do I fix the classes-not-found errors?

#### Answer

To fix the classes-not-found error, include classgen.jar and xmlparserv2.jar in your CLASSPATH.

### Class Generator: Cannot Create Root Object More than Once

#### Question

I've generated, from a DTD, a set of Java classes with "ClassGenerator". After, I've tried to create a Java application that uses these classes to create an XML file from data passed as arguments. I cannot create the root object, the object derived from CGDocument, more than one time because I obtain the following error message :

    oracle.xml.parser.XMLDOMException: Node doesn't belong to the current document

How do I handle the star operator (*). When the application starts I do not know how many times the element will appear. Thus I do not build a static loop where I make a sequence of "element.addNode()". The problem is that some of these will be empty and I will obtain an XML document with a set of empty elements with empty attributes.

#### Answer

You can create subsequent XML docs by calling the constructor each time. A well-formed XML document is not permitted to have more than one root node, therefore you cannot use the "*" on the element you are designating as the document root.

### Class Generator:Creating XML Files from Scratch, Using the DOM API

#### Question

I want to create an XML file using the DOM API. I dont want to create the XML file by typing in the text editor

    <xml>
      <future>is great</future>
    </xml>

instead, I want to create it using the DOM API. I do not know how to start! There are several examples of manipulating an XML file using the DOM once there is an input file, but not the other way round. That is, of creating the XML file from scratch (when there is no input file) using the DOM, once you know the "tagnames" and their "values".

#### Answer

The simplest way is download XML Class Generator for Java and give it a DTD of the XML document you want. It will create the DOM classes to programmatically create XML documents. There are samples included with the software.

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