Problems when generating classes from DTD

Discussion in 'Java' started by Jack Andersson, Dec 10, 2004.

  1. I'm using the code below to parse a DTD (which partly present below) and
    then generating corresponding classes.
    The problem I'm having now is that the generated classes seems to have the
    CompanyQuery as a subclass to CGDocument and all the others as subclasses to
    CGNode. However, my plan was to also have the CompanyResult as a subclass to
    CGDocument, but I can't understand what I'm doing wrong. Please note that
    the DTD is not present completely, because it is huge. Thus it may not
    possible to parse it directly.


    import java.io.File;
    import java.net.URL;
    import oracle.xml.parser.v2.DOMParser;
    import oracle.xml.parser.v2.DTD;
    import oracle.xml.parser.v2.XMLDocument;
    import oracle.xml.parser.XMLParser;
    import oracle.xml.classgen.*;


    public class SampleMain2
    {

    public SampleMain2()
    {
    }

    public static void main (String args[])
    {
    // PostCompanyQueryToTheca();
    CreateReturnMetaData();
    }

    public static void parseDTD ()
    {
    dtdFile = "MyDtd.dtd";
    String rootName = "CompanyQuery";
    try
    {
    // Instantiate the parser
    DOMParser parser = new DOMParser();
    //XMLParser parser = new oracle.xml.parser.v2.XMLParser();
    XMLDocument doc = null;
    oracle.xml.parser.v2.DTD dtd_v2 = null;
    oracle.xml.parser.DTD dtd_v1 = null;

    oracle.xml.parser.XMLParser xmlParser = new XMLParser();
    xmlParser.setErrorStream(System.out);
    xmlParser.setValidationMode(false);
    //xmlParser.parseDTD(fileToURL(dtdFile), rootName);
    dtd_v2 = new oracle.xml.parser.v2.DTD();
    dtd_v1 = (oracle.xml.parser.DTD) xmlParser.getDoctype();

    parser.setDebugMode(true);
    parser.setErrorStream(System.out);
    //parser.setSchemaValidationMode(false);
    //parser.setPreserveWhitespace(false);
    //parser.setValidationMode(false);

    parser.parseDTD(fileToURL(dtdFile), rootName);
    dtd_v2 = (DTD)parser.getDoctype();



    String doctype_name = null;

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

    // set generate comments to true
    generator.setGenerateComments(true);

    // set output directory
    generator.setOutputDirectory(".\\src\\DTDClasses\\");
    //generator.setOutputDirectory(".\\src\\");

    // set validating mode to true
    generator.setValidationMode(true);

    // generate java src
    generator.generate(dtd_v2, 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)
    {
    // According to the spec this could only happen if the file
    // protocol were not recognized.
    throw new Error("unexpected MalformedURLException");
    }
    }
    }


    <?xml version="1.0" encoding="UTF-8"?>
    <!-- Fråga mot företag -->
    <!ELEMENT CompanyQuery EMPTY>
    <!ATTLIST CompanyQuery CompEmployee CDATA #IMPLIED>
    <!ATTLIST CompanyQuery Employee CDATA #IMPLIED>

    <!-- Anrop till sökmotor, slår samman frågor -->
    <!ELEMENT CompanyCompose (CompanyQuery+)>
    <!ATTLIST CompanyCompose CustomerId CDATA #REQUIRED>
    <!ATTLIST CompanyCompose Duplicates (YES |
    VATNO |
    CFARNR |
    NAME |
    NAMELOCALITY |
    NAMEADDRESS |
    TELEPHONE)
    "YES">
    <!ATTLIST CompanyCompose SendRecords (NO|DIRECT|FILE)
    "NO">
    <!ATTLIST CompanyCompose FromRecord CDATA #IMPLIED>
    <!ATTLIST CompanyCompose ToRecord CDATA #IMPLIED>
    <!ATTLIST CompanyCompose MaxRecords CDATA #IMPLIED>
    <!ATTLIST CompanyCompose ID (0 | 1) "0">
    <!ATTLIST CompanyCompose VATNo (0 | 1) "0">

    <!-- Svar från CompanyCompose -->
    <!ELEMENT CompanyResult (CompanyResultCount,
    CompanyResultColumns?,
    CompanyResultRecord*)>
    <!ATTLIST CompanyResult ReturnCode (OK|ERROR) "OK">
    <!ATTLIST CompanyResult ErrorText CDATA #IMPLIED>
    <!ATTLIST CompanyResult DeliveryId CDATA #REQUIRED>
    <!ATTLIST CompanyResult FileName CDATA #REQUIRED>
    <!ATTLIST CompanyResult CustomerId CDATA #REQUIRED>

    <!-- Räknar antal poster -->
    <!ELEMENT CompanyResultCount EMPTY>
    <!ATTLIST CompanyResultCount Total CDATA #REQUIRED>
    <!ATTLIST CompanyResultCount FromRecord CDATA #REQUIRED>
    <!ATTLIST CompanyResultCount ToRecord CDATA #REQUIRED>
    <!ATTLIST CompanyResultCount WithTelephone CDATA #REQUIRED>
    <!ATTLIST CompanyResultCount WithTelefax CDATA #REQUIRED>

    <!-- Kolumner i utfilen -->
    <!ELEMENT CompanyResultColumns EMPTY>
    <!ATTLIST CompanyResultColumns ID (0 | 1) "0">
    <!ATTLIST CompanyResultColumns VATNo (0 | 1) "0">

    <!-- Resultatrecord -->
    <!ELEMENT CompanyResultRecord (ID?,
    VATNo?)>
     
    Jack Andersson, Dec 10, 2004
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kent Lichty
    Replies:
    0
    Views:
    854
    Kent Lichty
    Apr 16, 2004
  2. Joseph Tilian
    Replies:
    0
    Views:
    379
    Joseph Tilian
    Dec 21, 2004
  3. Ronald Fischer
    Replies:
    4
    Views:
    1,806
    Ronald Fischer
    Mar 17, 2005
  4. Ekim
    Replies:
    0
    Views:
    454
  5. test
    Replies:
    2
    Views:
    2,177
    Oliver Wong
    Jul 28, 2006
Loading...

Share This Page