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

Using XML Java Class Generator, 5 of 8

## Example Input DTD

The DTD file for employee data,`employee.dtd`, is used as the input to the class generator. Here, the DTD specifies that the XML document root is EMP and the row element is EMP_ROW. EMP consists of one or more EMP_ROWs.

Each EMP_ROW contains a required EMPNO attribute for the employee number, as well as several optional attributes such as ENAME for employee names, JOB for job type, MGR for manager, and so on. Optional attributes are followed by a "?" in the element definition.

### XML Java Class Generator DTD Example 1: Employee Data

    <!-- DTD for Employee Data -->
    <!ELEMENT EMP (EMP_ROW)*>
    <!ELEMENT EMP_ROW (EMPNO, ENAME?, JOB?, MGR?, HIREDATE?, SAL?,
                       COMM?, DEPTNO?)>
    <!ATTLIST EMP_ROW ROWNO CDATA #REQUIRED>
    <!ELEMENT EMPNO (#PCDATA)>
    <!ELEMENT ENAME (#PCDATA)>
    <!ELEMENT JOB (#PCDATA)>
    <!ELEMENT MGR (#PCDATA)>
    <!ELEMENT HIREDATE (#PCDATA)>
    <!ELEMENT SAL (#PCDATA)>
    <!ELEMENT COMM (#PCDATA)>
    <!ELEMENT DEPTNO (#PCDATA)>

### XML Java Class Generator Example 1: Processing the DTD to Generate Java Classes

The following example processes the DTD and generates the corresponding classes for elements in the DTD. Running the class generator on the DTD described in "XML Java Class Generator DTD Example 1: Employee Data" , creates Java classes for each element, EMP, EMP_ROW, EMPNO, ENAME, and so on.

A Java application can then use the methods defined on these classes to create a valid XML document containing employee data.

    import java.io.*;
    import java.net.*;
    import oracle.xml.parser.*;
    import oracle.xml.classgen.*;
    import org.w3c.dom.Element;

    public class SampleMain
    {
      public SampleMain()
      {
      }
      public static void main (String args[])
      {
         // validate arguments
        if (args.length < 1)
        {
          System.out.println("Usage: java SampleMain "+
                       "[-root <rootName>] <fileName>");
          System.out.println("fileName\t  Input file, XML document or " +
                       "external DTD file");
          System.out.println("-root <rootName>  Name of the root Element " +
                       "(required if the input file is an external DTD)");
          return ;
        }
        try // to open the External DTD File
        {
          // instantiate the parser
          XMLParser parser = new XMLParser();

          if (args.length == 3)
            parser.parseDTD(fileToURL(args[2]), args[1]);
          else
            parser.parse(fileToURL(args[0]));

          XMLDocument doc = parser.getDocument();
          DTD dtd = (DTD)doc.getDoctype();
          String doctype_name = null;

          if (args.length == 3)
          {
            doctype_name = args[1];
          }
          else
          {
            // get the Root Element name from the XMLDocument
            doctype_name = doc.getDocumentElement().getTagName();
          }

          // generate the Java files...
          ClassGenerator generator = new ClassGenerator();

          // set generate comments to true
          generator.setGenerateComments(true);
          // set output directory
          generator.setOutputDirectory(".");
          // set validating mode to true
          generator.setValidationMode(true);

          // generate java src
          generator.generate(dtd, doctype_name);

        }
        catch (Exception e)
        {
          System.out.println ("XML Class Generator: Error " + e.toString());
          e.printStackTrace();
        }
      }

      static public URL fileToURL(String sfile)
      {
        File file = new File(sfile);
        String path = file.getAbsolutePath();
        String fSep = System.getProperty("file.separator");
        if (fSep != null && fSep.length() == 1)
          path = path.replace(fSep.charAt(0), '/');
        if (path.length() > 0 && path.charAt(0) != '/')
          path = '/' + path;
        try
        {
          return new URL("file", null, path);
        }
        catch (java.net.MalformedURLException e)
        {
          // Can only happen if the file
          // protocol were not recognized
          throw new Error("unexpected MalformedURLException");
        }
      }
    }

### XML Java Class Generator Example 2: Creating a Valid XML Document from Java Classes

This Java example shows how generated methods can be used. Two row elements are created, emp_row1 and emp_row2. Elements for each column are also created, empno1, ename1, and so on.

To build an XML document tree, the various data elements are grouped by assigning them to each row element as tree nodes. Each row element is then added as a node to the document root element EMPLIST.

In this example, classes generated by the Class Generator, are in uppercase:

    import oracle.xml.classgen.*;
    import oracle.xml.parser.*;

    public class CreateEmployees
    {
      public static void main (String args[])
        {
          try
          {
            EMP EMPLIST = new EMP();
            DTD dtd = EMPLIST.getDTDNode(); // get static from base document

            // New employee emp_row1
            EMP_ROW emp_row1 = new EMP_ROW(1); // create row and set ROWNO
            EMPNO empno1 = new EMPNO("7654");
            ENAME ename1 = new ENAME("MARTIN");
            JOB job1 = new JOB("SALESMAN");
            MGR mgr1 = new MGR("7698");
            HIREDATE hiredate1 = new HIREDATE("1981-09-28 00:00:00.0");
            SAL sal1= new SAL("1250");
            COMM comm1= new COMM("1400");
            DEPTNO deptno1 = new DEPTNO("30");

            // New employee emp_row2
            EMP_ROW emp_row2 = new EMP_ROW(2); // create row and set ROWNO
            EMPNO empno2 = new EMPNO("7788");
            ENAME ename2 = new ENAME("SCOTT ");
            JOB job2 = new JOB("ANALYST ");
            MGR mgr1 = new MGR("7566");
            HIREDATE hiredate2 = new HIREDATE("1987-04-19 00:00:00.0");
            SAL sal2= new SAL("3000");
            COMM comm2= new COMM("");
            DEPTNO deptno2 = new DEPTNO("20");

            emp_row1.addnode(empno1); // Add data as tree nodes to emp_row1
            emp_row1.addnode(ename1);
            ...

            emp_row2.addnode(empno2); // Add data as tree nodes to emp_row2
            emp_row2.addnode(ename2);
            ...

            EMPLIST.addNode(emp1); // Add emp_row1 as tree node to
                                   // EMPLIST doc root
            EMPLIST.addNode(emp2); // Add emp_row2 as tree node to
                                   // EMPLIST doc root

            EMPLIST.validateContent();
            EMPLIST.print(System.out);
          }
        catch (Exception e)
        {
            System.out.println(e.toString());
            e.printStackTrace();
        }
      }
    }

### XML Java Class Generator Example 3: Resulting XML Document Built by a Java Application

The previous Java application, "XML Java Class Generator Example 2: Creating a Valid XML Document from Java Classes", creates an XML document similar to the following:

    <?xml version="1.0"?>
    <!DOCTYPE EMP SYSTEM "employee.dtd">
    <EMP>
      <EMP_ROW ROWNO = "1">
        <EMPNO>7654</EMPNO>
        <ENAME>MARTIN</ENAME>
        <JOB>SALESMAN</JOB>
        <MGR>7698</MGR>
        <HIREDATE>1981-09-28 00:00:00.0</HIREDATE>
        <SAL>1250</SAL>
        <COMM>1400</COMM>
        <DEPTNO>30</DEPTNO>
      /EMP_ROW>
      <EMP_ROW ROWNO = "2">
        <EMPNO>7788</EMPNO>
        <ENAME>SCOTT</ENAME>
        <JOB>ANALYST</JOB>
        <MGR>7566</MGR>
        <HIREDATE>1987-04-19 00:00:00.0</HIREDATE>
        <SAL>3000</SAL>
        <COMM></COMM>
        <DEPTNO>20</DEPTNO>
      </EMP_ROW>
    </EMP>

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